git Cheat-sheet

Note: This is an archived post. Information may not be relevant now.


First time set-up:

git config --global "Dipesh Acharya"
git config --global [email protected]

Push a branch

git push -u origin master
-u to set automatic tracking

Pulling a new branch from a remote repository

git fetch origin [remote-branch]:[new-local-branch]

See all changes

git log --oneline


Create new branch

git branch [new-branch-name]

View branches

git branch

Switch to a branch

git checkout [branch-name]

Merging from another branch

git merge [another-branch-name]

Merging from another branch creating a commit object for the merge and preventing fast-fowarding

git merge --no-ff [another-branch-name]

Delete a branch

git branch -d [branch-name]


Undo everything since last commit

git reset --hard

Undo all changes on [file] since last commit

git checkout [file]

Edit last commit message

git commit --amend

Restoring a deleted file from Git Repo:

git rev-list -n 1 HEAD -- [file_path]
git checkout [deleting_commit-id]^ -- [file_path]

Ignore file permission/mode changes:

git config core.filemode false

Roll back to an older commit

git reset --hard <tag/branch/commit id>


Tagging commits

git tag v1.9b
Tags can be used to refer to commits instead of commit id.

Tagging commits with message

git tag -a v1.4 -m 'message here'

Pushing tags

git push --tags


Creating git alias

git config --global 'commit -v'
git ci

Find out which branch contains a change

git branch --contains [commit-id]

Configure git to rebase automatically after pulls

git config branch.autosetuprebase always

Ignoring change in a file without deleting it from repo

git update-index --assume-unchanged <file>

Useful Links:

Git shortcuts like you’ve never seen before:

My bash aliases

Hub teaches git about GitHub

Git cheat sheets by Github

How to Handle a Pull Request from GitHub

A successful Git branching model