forked from holman/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-fix-origin
executable file
·34 lines (27 loc) · 1.02 KB
/
git-fix-origin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/bash
# Get the current directory
TARGET_DIR="$(pwd)"
# Find all Git repositories in the current directory
for dir in $(find . -name ".git" -type d); do
# Move to the Git repository directory
repo_dir=$(dirname "$dir")
cd "$repo_dir" || continue
# Check the current remote URL
remote_url=$(git remote get-url origin)
# Change the remote URL from HTTP to SSH
if [[ "$remote_url" == http* ]]; then
echo "Changing remote URL for repository: $repo_dir"
# Extract the GitHub user/repo part from the HTTP URL
git_user_repo=$(echo "$remote_url" | sed -E 's#https://github.com/(.*)\.git#\1#')
# Construct the new SSH URL
new_remote_url="[email protected]:$git_user_repo.git"
# Set the new remote URL
git remote set-url origin "$new_remote_url"
echo "Updated remote URL to: $new_remote_url"
else
echo "No change needed for repository: $repo_dir (current URL: $remote_url)"
fi
# Go back to the original target directory
cd "$TARGET_DIR" || exit
done
echo "Completed updating remote URLs."