git cheat sheet

What is Git?

Its the most widely used version control system today. Git makes collaborating with team members easy. This distributed version control system keeps track of all the changes made in project file. I use almost every single one of this cheatsheet commands in my everyday use. This cheat sheet features the most commonly used git commands for easy reference.  

Configure new git repository locally

$ git init
$ git clone [url]

NOTE: clone url can be found on your hosted git location

Check remote repositories mapped locally

$ git remote -v

Create new branch and switch

$ git checkout -b <new-branch> <existing-branch>

Rename local branch

$ git branch -m <NEW_FEATURE_BRANCH_NAME>

Delete local branch

$ git branch -d <FEATURE_BRANCH>

Commit changes without stagging

$ git commit -a -m "Your msg here”

Undo last git commit with reset

$ git reset HEAD~

Create empty git commit

$ git commit --allow-empty -m "Trigger notification"

Add changes to most recent commit

$ git commit --amend

Remove local branch and get new remote copy

$ git fetch && git reset --hard origin/FEATURE_BRANCH

Rebase on top of updated remote branch

$ git pull --rebase origin FEATURE_BRANCH

View git commit log

$ git log

Temporary commit changes locally

$ git stash
$ git stash list
$ git stash pop
$ git stash drop

Rebase interactively

$ git rebase -i master
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit

Undo a git rebase

Suppose you did a git rebase in your local branch but mistakenly rebased to an older branch and pushed changes to remote, then here is the solution to revert your changes and go back to the previous state.

Backup all your changes.

  1. Use git reflog to see all your previous operations. git log will show rebased and squashed changes only.
  2. Find out the commit where you want to go back to. Most probably this will be the commit before your rebase operation. You will see commit ids like HEAD@{16}
  3. Now reset your local branch to this commit.

$ git reset --hard HEAD@{16}

keep dabbling…

Leave a comment

Your email address will not be published. Required fields are marked *