I’ve been using Git full time for the past 4 years, and I wanted to share the most useful aliases that I’ve used along the way. Hopefully, it will be useful to somebody out there.

A Git alias is simply a way to add a shorthand for a common Git command or set of Git commands.

  1. When I started using Git, it didn’t take too long to reach about 70 local branches. Even though most of the branches were already merged and not used anymore, the only way to remove them was one by one, so I came up with the following alias:
    cleanup = "!git branch --merged master | grep -v '\* master' | xargs -n 1 git branch -d"

    Running the command git cleanup will delete all local branches that were merged into master but will keep the remote branches.

  2. The following alias can be used to force push a branch. Very useful when rebasing.
    fpush = "!git push -f origin origin"

    All you have to do is run the command git fpush

  3. Usually when you work in a branch and have some local changes you want to save temporary, you probably stash them. The stashes list could become confusing because stashes from all branches are saved in one place. The following alias will save your local changes into a commit with a custom message.
    save = "!git add -A && git command -m 'SAVEWORK'"

    Running the command git save will save all your local changes in a commit with the “SAVEWORK” message.

  4. This alias works great with the previous one. If you used the previous alias to save your local changes in a temporary commit, this alias will add the changes back to your staged section and will remove the temporary commit.
    undo = "!git reset HEAD~1 mixed"
  5. If you work in a branch and have many changes which you don’t want anymore, rather than using “hard reset” which removes the changes completely you can use this alias which will remove the changes but will save them in a hidden place. This way you can always go back to those changes. All you have to do is to run the git reflog command, find the SHA of the commit and reset it.
    wipe = "!git add -A && git commit -qm 'WIPE WORK' && git reset HEAD-1 -hard"
  6. To move all the commits from the current branch into a new branch you can use the following alias:
    migrate = "!f(){ CURRENT=$(git symbolic-ref --short HEAD); git checkout -b $1 && git branch --force $CURRENT ${3-'[email protected]{u}'} && git rebase --onto ${2-master} $CURRENT; }; f"

    This alias is a bit more complex and it requires 1 parameter which is the new branch name. To run the alias you can use the command git migrate new-branch-name or if you just want to migrate the last commit from master you can do something like: git migrate new-branch master HEAD~1.