git-usage
Some notes on Git
Difference between ^ and ~
commit^n choose the nth parent of the commit
HEAD^ = HEAD^1 = HEAD~ = HEAD~1
1 | G H I J |
See What’s the difference between HEAD^ and HEAD~ in Git?
Clone
Clone a repository with its submodules (--recurse-submodules)
1 | git clone --recurse-submodules git@domain.tld:repo.git` |
Add & Move & Remove
Add file in a interactive patch way (-p/--patch)
1 | git add -p <file> |
Move (Rename) file and then stage: git mv <filename1> <filename2>
Remove file: git rm <file>
Remove file in index only: git rm --cached <file>
Commit
- Automatically stage changes, add signoff, GPG-sign the commit:
git commit -asS - Fast modify last commit:
git commit --amend
Log
- Show log in a friendly format:
git -P log --pretty='format:%C(auto)%G?%d %h %an %ar %s' --graph --all - Show commits with diff patch (
-p/-u/--patch/--cc(dense)):git log -cc,git log --cc -1(For HEAD only) - Show commits for a user (
--author=/--committer=):git log --author=<pattern> - Grep in commit logs:
git log --grep=<pattern>
Diff
- Show a summary:
git -P diff --stat - Show diff of the last commit:
git -P diff HEAD HEAD~1orgit -P diff HEAD^!
Blame
Show each line of a file with last commit hash and author: git blame <file>
Stash
git stash
git stash pop
Branch & Remove
Create a branch and set its upstream (-t/--track): git branch <new-branch> <remote-branch>
Delete a branch (-d/--delete): git branch -d <branch>
Delete a remote branch (-d/--delete): git push <remote> -d <branch> / git push <remote> :<branch>
Rename a branch (-m/--move): git branch -m <old-branch> <new-branch>
To rename a remtoe branch, first delete the remote branch, then push a branch with new name: git push <remote> :<old-branch>; git push <remote> <new-branch>
Add remote and choose a branch to track: (-t/--track): git remote add -t <remote-branch> <remote-name> <url>
Tag
tag current HEAD: git tag <tag-name>
push tags (under refs/tags): git push --tags
Rebase
Modify last 10 commits: git rebase -i HEAD~10
pickdoesn’t change this commitdropdrop this commiteditedit this commitsquashmerge this commit with one commit above
Reset & Clean
Reset work tree and delete untracked files: git reset --hard HEAD; git clean -fdx
Reset:
--softonly change HEAD to<commit>--mixedreset index only.--hardreset both index and working tree--keepreset working tree only (If a file differ between<commit>andHEAD, and also has modified copy in index, this reset will be abort)
Clean:
-f --forceallowgit cleanto delete files or directories whileclean.requireForceis not set to false.-dNormally, when nois set, git cleanwill not recurse into untracked directories to removing too much. Specify-dto have it recurse into such directories as well.-xRemove ignored files and directories mentioned in.gitignore
Restore
Restore a file from specify commit: git restore -s <commit> <file>
Rvert
Create a revert commit (Useful when in a team): git revert <commit>
k
Submodule
Show submodule status: git submodule status
Add a submodule: git submodule add -b <remote-branch> [--name <name>] <url>
Initialize and update submodule to superproject’s recorded commit SHA-1: git submodule update --init or git submodule init; git submodule update
Update submodule to its remote-tracking branch: git submodule update --remote <submodule-name>
Specify how differences in submodule are shown when using git diff: git diff --submodule=[diff|log|short]
Push only when submodule commits are available on remote-tracking branch: git push --recurse-submodules=check
Push also submodule commits: git push --recurse-submodules=on-demand
Run command in each submodules: git submodule foreach <command>
More Greater Submodule for Git
然后使用git subrepo为开头
还可将子目录变为子仓库:
1 | git subrepo init <subdir> [-r <remote>] [-b <branch>] [--method <merge|rebase>] |
Git Cherry-pick
git cherry-pick用于把另一个本地分支的commit修改应用到当前分支
假设dev分支有一个hash为38361a68的commit
1 | $ git checkout master |
Git Format-patch
- Create patch files between two commit
1
git format-patch <r1>..<r2>
- Single commit patch
1
git format-patch -1 <r1>
- Create patch file since commit r1 (But doesn’t include this commit)
1
git format-patch <r1>
- Apply series of patches
1
git am *.patch
Limit memory usage of git-gc
1 | git config --global pack.windowMemory "100m" |