Skip to content

Instantly share code, notes, and snippets.

@arnauldvm
Last active January 27, 2024 10:00
Show Gist options
  • Save arnauldvm/dcec7ee043c25dce30dbae1b576f2102 to your computer and use it in GitHub Desktop.
Save arnauldvm/dcec7ee043c25dce30dbae1b576f2102 to your computer and use it in GitHub Desktop.
Git sync all local tracking branches with remotes
[alias]
tracking = "!f() { git for-each-ref --format '%(refname:short):%(upstream:short)' 'refs/heads' | egrep -v ':$'; }; f"
is-clean-workdir = "!f() { git diff --stat --exit-code || { echo \"Workdir dirty\"; exit 1; }; }; f"
is-clean-index = "!f() { git diff --stat --cached --exit-code || { echo \"Index dirty\"; exit 2; }; }; f"
is-clean = "!f() { git is-clean-workdir && git is-clean-index; }; f"
co-merge = "!f() { local=\"$1\"; remote=\"$2\"; git checkout \"$local\"; git merge --ff-only \"$remote\"; }; f"
current-branch = rev-parse --abbrev-ref HEAD
sync = "!f() { git is-clean || { echo Aborting sync.; exit 1; }; current=$(git current-branch); git fetch --all; git tracking | while IFS=: read local remote; do echo \"Merging $local with $remote\"; git co-merge \"$local\" \"$remote\"; done 3>&1 1>&2 2>&3 | egrep -i --color 'fatal|$' 3>&1 1>&2 2>&3; git checkout \"$current\"; }; f"
@arnauldvm
Copy link
Author

arnauldvm commented Jul 6, 2018

Solves this issue:

  • I want to sync all my local tracking branches at once
  • I want this operation to be safe. In other words, in case of local modifications or diverging branches, warn and don't touch the history (and do not tamper with uncommitted modifications)

After searching a lot for a simple solution, I ended up with my own (not so simple) solution, based on a series of git aliases (code to be added to the .gitconfig file).

⚠ This is fresh code, not yet so heavily tested.

Usage: git sync


Some explanations:

tracking = "!f() { git for-each-ref --format '%(refname:short):%(upstream:short)' 'refs/heads' | egrep -v ':$'; }; f"

→ This will retrieve all local tracking branches and the corresponding remote branch, colon separated.

is-clean-workdir = "!f() { git diff --stat --exit-code || { echo \"Workdir dirty\"; exit 1; }; }; f"
is-clean-index = "!f() { git diff --stat --cached --exit-code || { echo \"Index dirty\"; exit 2; }; }; f"
is-clean = "!f() { git is-clean-workdir && git is-clean-index; }; f"

→ These will verify that there are no pending changes in the workdir.

co-merge = "!f() { local=\"$1\"; remote=\"$2\"; git checkout \"$local\"; git merge --ff-only \"$remote\"; }; f"

→ This takes 2 arguments (a local branch and a remote branch), it will checkout the local branch and merge it with the remote branch. It uses --ff-only to avoid creating commit merges ⇒ In case of diverging local branch, it will issue a "fatal: Not possible to fast-forward, aborting." message on STDERR.

current-branch = rev-parse --abbrev-ref HEAD

→ Retrieve name of the branch currently checked out. Will be used to put the workdir back in its initial state, at the end.

sync = "!f() { git is-clean || { echo Aborting sync.; exit 1; }; current=$(git current-branch); git fetch --all; git tracking | while IFS=: read local remote; do echo \"Merging $local with $remote\"; git co-merge \"$local\" \"$remote\"; done 3>&1 1>&2 2>&3 | egrep -i --color 'fatal|$' 3>&1 1>&2 2>&3; git checkout \"$current\"; }; f"

→ That's the main part. Let's split this into parts:

git is-clean || { echo Aborting sync.; exit 1; }

→→ Aborts the command if there is pending work in the workdir.

current=$(git current-branch)

→→ Stores the name of the branch currently checked-out.

git fetch --all

→→ Sync remote branches from all remotes (--all).

git tracking | while IFS=: read local remote; do ...

→→ Iterates over each local tracking branch.

3>&1 1>&2 2>&3 | egrep -i --color 'fatal|$' 3>&1 1>&2 2>&3

→→ Highlights errors (containing the 'fatal' keyword). The "3>&1 1>&2 2>&3" magical formula exchanges STDOUT and STDERR, it is necessary to be able to pipe STDERR to the egrep command.

git checkout "$current"

→→ Restore the workdir to its initial state.

@arnauldvm
Copy link
Author

arnauldvm commented Jul 6, 2018

@a7madgamal
Copy link

thanks for sharing!

@a7madgamal
Copy link

I used your great script and added an automated stash before and after because I'm more lazy than you :D
https://gist.github.com/a7madgamal/f581763eaff88dd1d4ddc044cd4e6807

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment