BlogTop Daily Git Commands Cheat Sheet

31st July 2013

This is a cheeky little cheat sheet of a few useful GIT commands which I use on a daily basis.

Initialise Git

$ git init

Check which remotes are available

$ git remote -v

Add a remote

This is the reference to the remote origin so git knows where to push your code

$ git remote add origin git@path_to_repo.git

Stage Files ready for commit

$ git add .

OR if you've made updates including file deletions...

$ git add -u

Committing your changes...

$ git commit -m "This is a description of the stuff that I've done."

Pulling the latest changes from the remote repo

Under the covers this does a git fetch and a git merge

$ git pull origin master

N.B. I nearly always run the pull command before I push a change. This is so I can sort out any merging issues which might occur before pushing.

Rebase and re-run previous commits

Like git pull except instead of simply merging it re-runs all previous commits

$ git pull --rebase origin master

Pushing your changes to the remote respo

$ git push -u origin master

Check the status of the current local repo

$ git status

View a log of all the most recent commits

$ git log

Check what has changed in a file

$ git diff file_name.html

Ditch your changes and reset to last commit as per remote

$ git reset --hard origin/master

Revert to a local previous commit

$ git revert *commit_id*

Reverting changes to last commit

$ git revert HEAD

Removing files from Git

Remove files from the repo WITHOUT deleting the file. i.e. you've accidentally committed a local config file. Ensure they file is added to the .gitignore file otherwise it'll just get added again next time a commit is made.

$ rm --cached xxx.php

Assume unchanged

Temporaily ignore changes to a specific file.

$ git update-index --assume-unchanged path_to_file.html

Unassume unchanged

Reverse assume unchanged

$ git update-index --no-assume-unchanged path_to_file.html

Fixing a detached HEAD

$ git checkout -b new_branch_name
$ git commit -m "Fixing detached HEAD"
$ git checkout master
$ git merge new_branch_name

Stash unwanted changes

If you have changes you don't want or want to temporarily put them to the side (you can reinstate them at a later date

$ git stash

Let's Talk