Git - Cheatsheet

Posted on Fri 11 March 2016 in git

  • Initialize bare repository

    $ git init .

  • Amending the commit message which is not pushed

    $ git commit --amend

  • Force push amended commit message to a remote branch

    $ git push remote_name branch_name -f

  • Undo git add to files staged for git commit

    $ git reset HEAD file_name 
    or
    $ git rm --cached file_name 

    Usage: git reset HEAD a.sh or git rm --cached a.sh

    This command will remove a file named a.sh from the current index, the "about to be committed" area, without changing anything else.

  • Diff files which are under staging

    $ git diff --staged 

  • Diff between two commits

    $ git diff commit_1 commit_2

  • Diff between two branches

    $ git diff branch_1 branch_2

  • Push Local branch to central git repository

    $ git push remote_name branch_name

  • Create patch using git format-patch

    $ git format-patch master --stdout > fix_empty_poster.patch 

  • View first 3 commit messages in git log

    $ git log --pretty=oneline -3 

  • Adding signed-off in commit message

    $ git commit -s -m "commit_message" 

  • Using pull without unwanted merge commits

    $ git pull --rebase 

  • Deleting last commit in local repository

    $ git reset HEAD^ --hard 

  • Deleting last commit from remote repository

    $ git reset HEAD^ --hard 
    $ git push origin master -f 

  • Revert git pull

    $ git reset --hard 

  • Diff between lastest commit and last commit

    $ git diff HEAD^ HEAD

git