Skip to content

Instantly share code, notes, and snippets.

@slitayem
Last active February 18, 2020 17:27
Show Gist options
  • Save slitayem/6dfb4c7cc7b620e53874a64dc66d6e68 to your computer and use it in GitHub Desktop.
Save slitayem/6dfb4c7cc7b620e53874a64dc66d6e68 to your computer and use it in GitHub Desktop.
Git util commands

Push a remote branch to a new remote branch

  1. Add new target remote locally
git remote add <new-remote-name> <new-remote-utl>
git push <new-remote-name> <branch-to-push>:<new-remote-branch>

Get current branch name

git rev-parse --abbrev-ref HEAD

Tags

Delete local tag '1.0.0' git tag -d 1.0.0

Delete remote tag '1.0.0' git push origin :refs/tags/1.0.0

Alternative approach

git tag -d tagName

delete all local tags

git tag | xargs git tag -d

delete all remote tags

git tag -l | xargs -n 1 git push --delete origin

to delete a lot of tags wich are very similar (i.e. 2.0.0-1, 2.0.0-2, 2.0.0-3), so this is what I did:

git tag --list '2.0.0-*' | xargs -I % echo "git tag -d %; git push --delete origin %" | sh

Renaming locally and remotely a git branch

git branch -m old_branch new_branch # Rename branch locally
git push origin :old_branch # Delete the old branch
git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote

Delete a branch

  • Delete local branch git branch -d <branch-name>

  • Delete remote branch git push origin --delete <branch-name>

Update all local branches from remote

git fetch --all && for branch in $(git branch | sed '/*/{$q;h;d};$G' | tr -d '*') ; do git checkout $branch && git merge --ff-only || break ; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment