fozu
on
Apr 29, 20222022-04-29T00:00:00+08:00
Updated
Jun 92024-06-09T20:42:03+08:00
5 min read
1
2
| git diff 0da94be 59ff30c > my.patch
git diff k73ud^..dj374
|
1
| git diff oldCommit..newCommit
|
1
| git diff [--options] <commit> <commit> [--] [<path>...]
|
1
2
3
| git diff HEAD^^ HEAD main.c
git diff HEAD^^..HEAD -- main.c
git diff HEAD~2 HEAD -- main.c
|
1
| git branch branch_name --set-upstream-to your_new_remote/branch_name
|
or
1
| git branch branch_name -u your_new_remote/branch_name
|
1
2
| git remote set-head origin main
git push origin --delete master
|
1
2
| git show branch:file
git show branch:file > exported_file
|
Git Pull
1
2
3
4
5
6
7
8
9
10
11
12
| # rebase
git pull --rebase
# merge
git pull --no-rebase
# merge and commit
git pull --commit
# merge and stop just before creating a merge commit
git pull --no-commit
# Invoke an editor before committing successful mechanical merge
git pull --edit
# accept the auto-generated message (this is generally discouraged).
git pull --no-edit
|
1
2
| git diff ..master path/to/file
git diff <hash1> <hash2> <filename>
|
1
| git checkout FROM_BRANCH_NAME path/to/file
|
Simplify. Cherry-pick the commits. Don’t cherry-pick the merge.
1
2
| # will use the first parent listed in the merge as the base.
git cherry-pick -m 1 fd9f578
|
1
2
3
4
| git cat-file -p $commit_id
git show --first-parent
git log --first-parent
git show --parents
|
1
| git merge <name-of-branch> --no-commit --no-ff
|
1
| git config -l --show-scope
|
1
2
3
4
5
| # destroy current head pointed commit and throw away any uncommitted changes
git reset --hard HEAD^
git reset --hard HEAD~1
git reset HEAD^
|
1
| git remote set-url origin new.git.url/here
|
1
2
3
4
| # delete remote tag
git push origin :refs/tags/v0.1.0
# delete locally
git tag -d v0.1.0
|
1
2
3
4
5
6
| git log --follow --diff-filter=A --find-renames=40% foo.js
git log --diff-filter=A -- foo.js
# alias it
git config --global alias.whenadded 'log --diff-filter=A'
|
--diff-filter=[(A|C|D|M|R|T|U|X|B)…[\*]]
Select only files that are Added (A
), Copied (C
), Deleted (D
), Modified (M
), Renamed (R
), have their type (i.e. regular file, symlink, submodule, …) changed (T
), are Unmerged (U
), are Unknown (X
), or have had their pairing Broken (B
). Any combination of the filter characters (including none) can be used. When *
(All-or-none) is added to the combination, all paths are selected if there is any file that matches other criteria in the comparison; if there is no file that matches other criteria, nothing is selected.
1
2
3
4
5
6
7
| git pull -s recursive -X theirs <remoterepo or other repo>
git pull -X theirs
# If you're already in conflicted state, and you want to just accept all of theirs:
git checkout --theirs path/to/file
git checkout --ours .
git add .
|
-X
performs a regular recursive merge, resolving any conflicts using the chosen side, whereas -s ours
changes the merge to just completely ignore the other side.
1
2
3
4
5
| git checkout branchA
# accept theris
git merge -X theirs branchB
# accept others
git pull --strategy=theirs remote_branch
|
1
2
| fatal: bad object refs/remotes/origin/{branchname}
fatal: failed to run repack
|
1
2
| rm -rf .git/refs/remotes/origin/{branchname}
git gc --aggressive --prune=now
|
1
| git rebase -i --root -x "git commit --amend --author 'chenyi <747852289@qq.com>' --no-edit"
|
1
2
3
4
| mv oldpath newpath
git rm oldpath
git add newpath
git submodule sync
|
1
| git submodule set-url [--] <path> <newurl>
|
1
| git remote prune origin --dry-run
|
1
2
3
4
5
6
7
8
9
10
| 0. mv a/submodule a/submodule_tmp
1. git submodule deinit -f -- a/submodule
2. rm -rf .git/modules/a/submodule
3. git rm -f a/submodule
# Note: a/submodule (no trailing slash)
# or, if you want to leave it in your working tree and have done step 0
3. git rm --cached a/submodule
3bis mv a/submodule_tmp a/submodule
|
1
2
3
4
5
6
| git checkout c5f567 -- file1/to/restore file2/to/restore
git checkout c5f567~1 -- file1/to/restore file2/to/restore
git diff <commit hash> <filename>
# You may need to use the --hard option if you have local modifications.
git reset <commit hash> <filename>
|