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.
1 2 3 |
sudo apt-get install git -y |
Clone repository
git clone URL
Downloads whole repository.
1 2 3 4 |
git clone "https://LOGIN:PASSWORD@bitbucket.org/some/repo.git" git clone "https://github.com/AlienTechLAB/AlienRenderer.git" |
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.
1 2 3 4 5 6 |
git config user.name "Some User" git config user.email "some@email.com" git config --global user.name "Some User" git config --global user.email "some@email.com" |
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.
1 2 3 4 5 |
git add "src/foo.cpp" # Add "src/foo.cpp" file. git add -A # Add all files. git add -f "res/picture.jpg" # Add binary file. |
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.
1 2 3 |
git commit -m "Message" |
git push
Sends all commits to the server.
1 2 3 4 |
git push --set-upstream origin branch_name # Sends new branch to the remote repository. git push origin --delete branch_name # Deletes remote branch. |
git log
Shows history of commits.
git checkout
Checkout allows you to change or create branch. It also allows to revert changes in file.
1 2 3 4 5 6 |
git checkout some_ther_branch # Switch branch. git checkout -b new_branch # Create new branch. git checkout FILE_NAME # Reverts changes in this file git checkout 39bc718 # Checkouts to concrete commit. |
git merge
It merges other branch into the current one.
1 2 3 |
git merge master # Merges master branch into the current branch. |
git branch
That command is for branch operations.
1 2 3 4 5 6 |
git branch # Displays local branches and highlights with green color the current branch. git branch -a # Displays local and and remote branches. git branch -d branch_to_delete # Deletes "branch_to_delete". Use it with git push origin: branch_to_delete git branch -r # Lists remote branches." |
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.
1 2 3 4 5 6 |
git stash list # List of stashed changes git stash show # Show all stashes. git stash apply # Brings back those changes git stash clear # Remove all stashes |
git reset
This command is used to revert changes.
1 2 3 4 5 |
git reset --hard # Revert all changes since last commit git reset HEAD some_file.cpp # "undo" git add some_file.cpp. This file is removed from files to commit. git reset HEAD~ # "undo" last commit. |
git diff
In general this command is for displaying changes that has been made.
1 2 3 4 5 6 7 |
git diff some_file.cpp # Shows changes made in this file. git diff-tree --no-commit-id --name-only -r bd61ad98 # Show file changes in particular (bd61ad98) commit. git diff 270b0c50 (predecessor) 6d6c6494 (successor) # Shows differences between commits. git diff --name-status branch1..branch2 # List of files (both branches MUST be listed by git branch command) git diff mybranch master -- myfile.cs # Shows differences in one file between branches. |
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.
1 2 3 4 |
git submodule add https://github.com/aws/aws-sdk-cpp aws-sdk-cpp # Adds other git repository as sub-project. git submodule update --init --recursive # Update submodules. It pulls changes in submodule repositories. |
git remote
Sometimes I don't remember the url address where repo comes from. Command below prints this url address.
1 2 3 |
git remote get-url origin |
git rm
Remove files.
1 2 3 |
git rm -r --cached file_name # Removes file from remote repository. |
cleaning cache
1 2 3 4 5 6 |
git rm -r --cached . git add . git commit -m "git cache cleared" git push |
Remove all changes that are not staged.
1 2 3 |
git clean -- . |
.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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#OS junk files [Tt]humbs.db *.DS_Store #Visual Studio files *.opendb *.db *.[Oo]bj *.user *.aps *.pch *.vspscc *.vssscc *_i.c *_p.c *.ncb *.suo *.tlb *.tlh *.bak *.[Cc]ache *.ilk *.log *.lib *.sbr *.sdf *.pyc *.xml ipch/ obj/ [Bb]in [Dd]ebug*/ [Rr]elease*/ Ankh.NoLoad #Tooling _ReSharper*/ *.resharper [Tt]est[Rr]esult* #Project files [Bb]uild/ #Subversion files .svn # Office Temp Files ~$* |