CLI
# show repository info git describe git config -l # show commit info git show <commit-id> # show diff for one file git diff <FILE> git --no-pager diff # commit only one file git commit -m 'my comment' path/to/file # create Git repository git init # change upstream server git remote set-url origin git@git.example.com:foo/bar.git # add all files git add . # commit git commit -m "initial project version" # revert git revert <commit> # cancel merge git merge --abort # list tag git tag -l # get files changed in commit git diff-tree --no-commit-id --name-only -r <COMMIT_ID>
checkout
# checkout over SSH git clone git@github.com:foo/bar.git git clone -b <branch> <remote_repo> # checkout branch git clone --branch "dev" https://git.example.com/foo/bar /target/directory # pull single file git checkout filename # checkout file from specific commit git checkout f08a63ff4fa7b8479f8c698e5998ee1afcac3a4e bar.txt git checkout dccdc82e -- path/to/file # pull single file from remote git checkout origin/master -- path/to/file # run git pull in specific directory git -C /var/www/html pull # pull all files from remote git checkout origin/dev -- . # sync changes from other branch git rebase master git rebase brach1 # reset changes git reset --hard master # checkout to checkout to specific folder git clone https://github.com/panticz/installit.git /path/to/folder # create tag git tag -a v1.4 -m 'version 1.4' git push --tags git push origin <tag_name> # remove tag git tag -d tag123 git push origin :refs/tags/tag123 # checkout tag git clone --branch bar-1.0.4 git@git.example.com:foo/ansible.git /tmp/bar-1.0.4 # ignore certifcate git -c http.sslVerify=false pull # git pull single file git checkout origin/master -- file_name # checkout commit git checkout commit_hash # global gitignore git config --global core.excludesfile ~/.gitignore_global
Branch
# shows all local and remote branches git branch -a # shows only remote branches git branch -r # reset branch git reset --hard origin/branch123 git reset --hard <tag/branch/commit id> # rollback to revision git checkout 96fe40ded8277725d244aac83c42256ad554cc3b . # create branch git checkout -b branch_name # switch branch git checkout branch_name git checkout master # checkout branch git clone <url> --branch <branch> --single-branch [<folder>] # diff all files betwen braches git diff dev..stage # diff singe file between branches git checkout --track origin/dev git checkout --track origin/stage git diff dev..stage file1 # diff branches and show file name only git diff --name-only dev..stage # diff differnet fiels in different branches git diff branch1:file branch2:file # test git reset file/to/overwrite # search for string git grep "string/regexp" $(git rev-list --all) # grep git grep foo HEAD # remove last commit git reset HEAD~1 # reset to latest revision git reset --hard # reset to commit-id git reset --hard <commit-id> git push origin HEAD --force
Cherry-pick from another repository
# Cloning our fork git clone git clone git@github.com:ifad/rest-client.git # Adding (as "endel") the repo from we want to cherry-pick git remote add endel git://github.com/endel/rest-client.git # Fetch their branches git fetch endel # List their commits git log endel/master # Cherry-pick the commit we need git cherry-pick 97fedac
stash
git stash list git stash drop "stash@{0}"
branch
# diff between branches git diff master # show branches git branch # push local Git branch to remote master branch git push origin local_branch_1:master # delete local branch git branch -d local_branch_name # delete remote branch git push origin :remote_branch_name # delete directory / file from git repository without removing from disk git rm --cached -r DIR1 # ignore ./.gitignore # user environment settings export GIT_COMMITTER_NAME="foo" export GIT_COMMITTER_EMAIL="foo.bar@example.com" export GIT_AUTHOR_NAME="foo" export GIT_AUTHOR_EMAIL="foo.bar@example.com" export GIT_USERNAME="foo" export GIT_PASSWORD="foo.bar@example.com" # global setup git config --global user.name "first_name last_name" git config --global user.email "user@example.com" git config --global core.autocrlf true # create a new repository git clone git@example.com:repository_group/repository_name.git cd repository_name touch README.md git add README.md git commit -m "add README" git push -u origin master # search for string in commit git grep foo $(git rev-list --all) # check out single file git archive --remote=git@git.example.com:foo/bar.git HEAD:path1/file_or_dir target_file_name | tar --extract # set proxy http://cms-sw.github.io/tutorial-proxy.html git config --global http.proxy $http_proxy git config --global https.proxy $https_proxy # undo changes on file git checkout -- file # inclue a commit git cherry-pick commit1 http.sslVerify http.sslCAInfo http.sslCAPath http.sslCert http.sslKey http.sslCertPasswordProtected # pass GIT settings over SSH https://cweiske.de/tagebuch/carry-git-settings.htm GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example -F /dev/null" git clone example git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -F /dev/null"
Add existing folder or Git repository
cd existing_folder git init git remote add origin git@example.com:repository_group/repository_name.git git add . git commit git push -u origin master
Submodule
https://chrisjean.com/git-submodules-adding-using-removing-and-updating/ git rm --cached roles/icinga-client git submodule add git@git.example.com:foo/ansible-roles/icinga-client.git roles/icinga-client # update submodule git submodule update --recursive --remote
.gitignore
# ignore all execpt for... * !.gitignore !dnsmasq.conf !dnsmasq.d/ !dnsmasq.d/**
Git ext:: remote helper
https://git-scm.com/docs/git-remote-ext
git pull "ext::ssh -t B ssh git-server %S '/path/to/repository'"
git hooks post-merge example
#!/bin/bash # check for modified files for FILE in $(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD); do case "${FILE}" in *.json) # Repository configuration changed: update configuration ~/my_repository/scripts/json_update.sh ;; cronjobs/crontab) # Crontab changed: update user cronjobs ~/my_repository/scripts/crontab_update.sh ;; esac done >