GIT Commands

Below you will find most important (in my opinion) git commands. I use git only from terminal on a daily basis, but still forget some of the commands or it’s parameters. Hence this cheat sheet.
 

How to install git?

 
Many Linux distros don't have preinstalled git. You have to install it manually. Example below is for Linux Mint 18.2 "Sonya" - Cinnamon (64-bit) but should works on other Debian based distros too.

 

Clone repository

 
git clone URL
Downloads whole repository.

 
git config
I use it to set user's name and user's e-mail. That data are identifing author of every commit. When you type git log command, it displays history of commits and who did it. You can set one user for all repos using --global, but it is better to set user's name and e-mail for every project separately.

 

Normal workflow

 
git pull
Downloads only changes for that branch that someone else did in the meantime.
 
git status
Shows changes you have made. Prints list of modified / added / deleted files. Files ready to commit are displayed in green color, the rest of the files in red.
 
git add
This command is used to register files as ready to commit. New file, modified or deleted ones.

 
git commit
All changed files (added or deleted too) are now set as ready to commit. -m parameter is for message for that particular commit.

 
git push
Sends all commits to the server.

 
git log
Shows history of commits.
 
git checkout
Checkout allows you to change or create branch. It also allows to revert changes in file.

 
git merge
It merges other branch into the current one.

 
git branch
That command is for branch operations.

 
git stash
Sometimes it is necessary to leave what you are doing and start doing something else - more important. When you are in the middle of work and it is not ready to commit and at the same time it has some value so It would be loss to reject that, then you should use stash command. It saves current changes but does not commit it.

 
git reset
This command is used to revert changes.

 
git diff
In general this command is for displaying changes that has been made.

 
git submodule
Sometimes you want to use other project (say library) in yours. It's good idea to make "link" to this other projects repo. This is what submodule is for.

 
git remote
Sometimes I don't remember the url address where repo comes from. Command below prints this url address.

 
git rm
Remove files.

 
cleaning cache

 
Remove all changes that are not staged.

 

.gitignore

 
When working on project, various tools generates their own files, which not necessarily should be included in repository. To exclude those files use .gitignore file. Create normal text file and name it .gitignore. This file should be in project's root directory (next to .git directory). And then just list files you want to exclude. Below, you'll find .gitignore I use for Visual Studio projects.
 
.gitignore