Although there are enough resources on the web about Git, I will keep this one for my own reference. Minimal Git version required 1.7.2.
- index: staging area (Imagine you are loading sand into the truck with bucket. Well, the bucket is like index and truck like a repository :)
- <sha1>: sha1 hash of commit
- <file>: path to the file (path/to/file.ext)
- <branch>: branch name
- <repository>: remote repository name
git log -G <regex> -- <path>
git log -S <string> -- <path>
Useful options:
-i, --regexp-ignore-case
Match the regular expression limiting patterns without regard to letter case.
-E, --extended-regexp
Consider the limiting patterns to be extended regular expressions instead of the default basic regular expressions.
-F, --fixed-strings
Consider the limiting patterns to be fixed strings (don't interpret pattern as a regular expression).
--perl-regexp
Consider the limiting patterns to be Perl-compatible regular expressions. Requires libpcre to be compiled in.
git log --diff-filter=D --summary
git checkout <sha1>^ <file>
git checkout <branch> -- <file>
git show <sha1>^:<file>
git diff -M <branch>
git log -p <file>
git cherry -v <upstream_branch> <new_branch>
git log <upstream_branch>..<new_branch>
git log --name-only
git log --name-status
git log --stat
git describe --exact-match --abbrev=0
git describe --abbrev=0 --tags
git name-rev --name-only <sha1>
git branch --contains <sha1>
git add --patch <file>
- y: stage this chunk
- n: do not stage this chunk
- s: split this chunk into smaller chunks
- e: edit this chunk
If not provided, Git uses HEAD
as the new branch start point.
git checkout -b <branch>
git checkout -b <branch> <start>
or
git branch <branch>
git checkout <branch>
git branch <branch> <start>
git checkout <branch> <start>
Delete already merged branch
git branch -d <branch>
Force branch deletion
git branch -D <branch>
Pick from start <sha1>
commit till end <sha1>
commit.
git cherry-pick <sha1>..<sha1>
By default Git will create a patch for every commit. Use --stdout > <patch>.patch
for combined patch.
Create patches for the last N
commits (each commit in it's own patch).
git format-patch HEAD~<N>
Create patches containing all commits from the current branch against another <branch>
branch (each commit in it's own patch).
git format-patch <branch>
Creating combined patch.
git format-patch HEAD~<N> --stdout > <patch>.patch
git format-patch <branch> --stdout > <patch>.patch
Check what changes are in the patch
git apply --stat <patch>.patch
Test the patch before applying
git apply --check <patch>.patch
Apply patch
git am [--signoff] < <patch>.patch
git reset contains great explanation and examples.
--soft
option will keep files in the index.
git reset [--soft] HEAD^
git reset --hard
git reset --merge ORIG_HEAD
git revert <sha1>
git checkout <sha1>^ -- <file>
git checkout -b <branch> <repository>/<branch>
git branch --set-upstream <branch> <repository>/<branch>
git push <repository> :heads/<branch>
git push <repository> :<branch>
git push <repository> --delete <branch>
git remote prune <repository>
git remote set-url <repository> https://example.com/repo.git
--squash
do not preserve history (squash history)
Add subtree
git subtree add --prefix <directory> <url> <branch> [--squash]
Pull subtree
git subtree pull --prefix <directory> <url> <branch> [--squash]
Add remote
git remote add -f <name> <url>
Add subtree
git subtree add --prefix <directory> <remote> <branch> [--squash]
Pull subtree changes
git fetch <remote> <branch>
git subtree pull --prefix <directory> <remote> <branch> [--squash]
Push subtree changes
git subtree push --prefix=<directory> <remote> <branch>
git submodule foreach 'git checkout master && git pull origin master'
Edit the .gitmodules file, then run:
git submodule sync
- remove the submodule's entry in the .gitmodules file
- remove the submodule's entry in the .git/config
- run
git rm –cached path/to/module
- without a trailing slash! - remove the submodule from the filesystem, run
rm -rf path/to/module/
- commit changes