git cheatsheet

General settings

Change the default editor

$ git config --global core.editor "vim"


View commit history as a graph

$ git config --global 'log --oneline --graph --decorate --all'


Delete all local branches

$ git branch | grep -v "master" | xargs git branch -D 

Reference: Remove all your local git branches but keep master - Coderwall

Rename branches

Rename current branch

$ git branch -m <new_name>

Rename another branch

$ git branch -m <branch_name> <new_name>


Combine multiple commits

Step 1: Use git rebase in interactive mode with the parent commit (the commit you would like to start merging from).

$ git rebase -i <parent_commit_it>


$ git rebase -i HEAD~3

This will attempt to merge the last three commits into one.

Step 2: In the next screen, pick the first commit and squash the rest.

Step 3: Edit the commit message.

Reference: How to Combine Multiple Git Commits into One - w3docs

Move commits between branches

Move a commit in another branch to the current branch (cherry-pick)

$ git cherry-pick <commit_id>

Move contents of a commit to staging index

$ git cherry-pick -n <commit_id>

Reverting merge commits

The -m option has to be specified when reverting merge commits.

Merge commits have multiple parents and -m specifies which parent commit to revert to.

Reference: How to revert a merge commit that's already pushed to remote branch? - Stack Overflow