Git Quick Tutorial, Cheat Sheet

The following is meant to be a quick cheatsheet to recap some Git commands.

Basic flow

An example basic flow of commands:
git initto create an empty Git repository in a .git folder.
git statusto see which files are not tracked, changes, conflicts etc.
git add .to add all files to the staging area (a.k.a. “index”).
(if you modify a file now and want to commit those changes, don’t forget to git add it again before committing)
git reset file.txtto undo that last add command for file.txt, so now it’s not tracked.
git commit -m "my commit remark" creates a new commit object with the staged changes in the Git repository.

Deleting files

Create some file and commit it: touch deleteme.txt git add . git commit -m "added deleteme.txt" Will commit the deleteme.txt file
Try to delete (will FAIL): rm deleteme.txt git add . git commit -m "will fail" Will get a no changes to commit message, and the deleteme.txt file will still be kept in the repository…
How to delete (alternative 1): git add -A . git commit -m "success" The -A (or equivalently --all) compares changes in the working tree as well as the index, so the file deleteme.txt will now be deleted in the repository as well.
How to delete (alternaive 2): git commit -a -m "success" The -a (or equivalently --all) tells the commit command to automatically stage files that have been modified and deleted, so the file deleteme.txt will now be deleted in the repository as well.
How to delete (alternaive 3): Instead of the rm deleteme.txt above, could instead: git rm deleteme.txt git commit -m "success" The rm command removes the file and records its deletion in Git, so after the commit command the deleteme.txt will be deleted in the repository as well.


git diff shows the changes in the working tree not yet staged for the next commit. git diff --cached shows the changes between the index and your last commit; what you would be committing if you run “git commit” without “-a” option. In any case, it ignores any non-tracked files.
echo "some text" > file.txt git add file.txt git commit -m "remark1" Suppose we add a file.txt and commit it.
Now modify it: echo "more text" >> file.txt Modified, but still didn’t add it to staging, i.e. we didn’t run: git add file.txt
git diff will show us the differences we have in that tracked file! Example diff output: diff --git a/file.txt b/file.txt index 477f637..f8217a9 100644 --- a/file.txt +++ b/file.txt @@ -1 +1,2 @@ "some text" +"more text"
But after adding the changes there will be no diff: git add file.txt git diff So the git diff will not print anything now. If you want to see the changes between the index and the last commit, use git diff --cached (will produce the same diff output as above)

Remotes and more

git remote List the existing remotes whose branches you track.
git remote -v A bit more verbose, show remote url after name.
– Add a remote repository: Use ‘git remote add’ with some remote name (usually “origin” is the name of the main remote) and repository URL: git remote add origin – Now can push local changes remotely to our ‘origin’ repo (on GitHub): The name of our remote is ‘origin’. The default branch name is ‘master’. git push -u origin master (the -u tells Git to remember the parameters, so that next time we can simply run git push and Git will know what to do) – Suppose others worked on our project and committed stuff. We can check for changes on our GitHub repository and pull down any new changes by running: git pull origin master – If we want the diff of our most recent commit, which we can refer to using the HEAD pointer: git diff HEAD – Another great use for diff is looking at changes within files that have already been staged/cached (added, but not committed yet): git diff --cached (--cached is same as --staged) – Other example for git diff: git diff v2.5:Makefile – To unstage files: git reset octofamily/octodog.txt – To revert the working tree to the state of some branch or specific paths: git checkout master (checks out the master branch (switches branch)) git checkout master~2 Makefile (takes a file out of another commit. Reverts the Makefile to two revisions back) – Now suppose we accidentally ran ‘rm -f hello.c‘. Then we could restore it from the index: git checkout hello.c (but in case we have a branch called hello.c, this will be understood as reverting to that branch!!! So better to use the following:) git checkout -- hello.c – Branching out: git branch my_branch – To see the branches: (with “*” at the current branch) git branch – To switch to another branch: (also discussed above) git checkout my_branch – In that branch, suppose we remove some files from both the disk and from the staging/index: git rm '*.txt' – And commit the change: git commit -m "cleaned up stuff" – Now we want to copy (merge) changes from my_branch back into the master branch: git checkout master git merge my_branch – To delete a branch: git branch -d my_branch – Finally, can again push it to the remote repo: git push
git log Shows the recent commits etc.

Other resources