Basic commands #
git init
- Init local repogit status
- displays status of the repositorygit diff --staged
- diff to staged (commited) files to local repogit add -all
- Add all changed, new and removed filesgit commit -m "Commit message"
- performs commit of added files to the local repositorygit remote add origin https://github.com/kospiotr/sample-repo.git
- add remote repository link to local repositorygit push origin master
- transfers changes from local to remote repositoriesgit push -u origin master
- remember settings that next time you can use onlygit push
git push origin --all
- push everything to remote repository including branchesgit add --all & git commit -m "Update" & git push
- add all, commit and push togethergit checkout
discard changes in working directorygit reset --hard
- all changes will be revertedgit clean -df
- clean all untracked directories and filesgit clean -df && git reset --hard && git pull
- discards any change and updates branchgit branch | grep -v "master" | xargs git branch -d
- removes all branchces except master and current
Excluding files from repository #
Reference: http://git-scm.com/docs/gitignore
To exclude certains directories and files you need to place them in .gitignore
file. Here you can find some examples for popular project structures: https://github.com/github/gitignore
The purpose of gitignore files is to ensure that certain files not tracked by Git remain untracked.
To ignore uncommitted changes in a file that is already tracked, use git update-index --assume-unchanged
.
To stop tracking a file that is currently tracked, use git rm --cached
.
Push to all git remotes with the one command #
Create an all remote with several repo URLs to its name:
git remote add all [url1]
git remote set-url --add all [url2]
git remote set-url --add all [url3]
Then just git push all --all
.
This is how it looks in .git/config:
[remote "all"]
url = [url1]
url = [url2]
url = [url3]
Source: http://stackoverflow.com/a/5785618/276948
Branching #
git branch -a
- display branchesgit branch --merged
- display only merged branchesgit branch --no-merged
- display only no merged branchesgit branch -df <branchName>
- force to delete branch locallygit push origin --delete <branchName>
- deletes branch remotellygit pull -a --prune
- prunes all local branches
Patching #
git format-patch -2 HEAD
- create patch out of 2 previous commits to autogenerated filegit format-patch -2 HEAD --stdout > fix.patch
- create patch out of 2 previous commits to filefix.patch
git apply --stat fix.patch
displays stats about patchgit apply --check fix.patch
checks patch if possible to applygit apply fix.patch
- applies patch
Reverting changes #
git reset --soft HEAD^
- uncommiting latest commit and make all changes unstaged. It works when the commit is not pushed to upstream
Certificates #
Generate private / public keys #
ssh-keygen
To use ssh with remote repositories you need to publish your public key to this services. It can be localized here:
cat ~/.ssh/id_rsa.pub
Go to: https://github.com/settings/keys and add the above content. Test connectivity:
ssh -T git@github.com
Git tools #
Smartgit #
[SmartGit] is a graphical Git and Mercurial client. It runs on Linux, Mac OS X (10.5 or newer) and Windows (XP or newer).
sudo add-apt-repository ppa:eugenesan/ppa
sudo apt-get update
sudo apt-get install smartgithg
Github tools #
- Travis - CI
- waffle.io - Stories board
- shields.io - icons
- coveralls.io - Test Coverage History & Statistics
- other
External resources #
- Cheat cheet
- [Git Recipes - Wlodzimierz Gajda](../resources/books/Git Recipes - Wlodzimierz Gajda.pdf)
- http://try.github.io
- https://help.github.com/articles/generating-ssh-keys
- Pro GIT book pdf epub