Home git troubleshooting
Post
Cancel

git troubleshooting

Show diff between commits

1
2
git diff 0da94be  59ff30c > my.patch
git diff k73ud^..dj374
1
git diff oldCommit..newCommit

How do I diff the same file between two different commits on the same branch?

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

How to change the remote a branch is tracking?

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

Delete the remote master branch

1
2
git remote set-head origin main
git push origin --delete master

View a file in a different Git branch without changing branches

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

How to compare files from two different branches

1
2
git diff ..master path/to/file
git diff <hash1> <hash2> <filename>

Overwrite single file in my current branch with the same file in the main branch?

1
git checkout FROM_BRANCH_NAME path/to/file

git cherry-pick says “…38c74d is a merge but no -m option was given”

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

Telling if a Git commit is a Merge/Revert commit

1
2
3
4
git cat-file -p $commit_id
git show --first-parent
git log --first-parent
git show --parents

Git merge without auto commit

1
git merge <name-of-branch> --no-commit --no-ff

Where do the settings in my Git configuration come from?

1
git config -l --show-scope

How do I undo the most recent local commits in Git?

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^

How do I change the URI (URL) for a remote Git repository?

1
git remote set-url origin new.git.url/here

How can I delete a remote tag?

1
2
3
4
# delete remote tag
git push origin :refs/tags/v0.1.0
# delete locally
git tag -d v0.1.0

How to find the commit in which a given file was added?

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

--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.

Resolve Git merge conflicts in favor of their changes during a pull

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 .

Is there a “theirs” version of “git merge -s ours”?

–strategy-option (short form -X)

-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

fatal: bad object xxx

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

warning: remote HEAD refers to nonexistent ref, unable to checkout

Change git email for previous commits

1
git rebase -i --root -x "git commit --amend --author 'chenyi <747852289@qq.com>' --no-edit"

Rename a git submodule

1
2
3
4
mv oldpath newpath
git rm oldpath
git add newpath
git submodule sync

How to change the remote repository for a git submodule?

1
git submodule set-url [--] <path> <newurl>

Delete the local reference to a remote branch in Git

1
git remote prune origin --dry-run

How do I remove a submodule?

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

How can I reset or revert a file to a specific revision?

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>

How to revert initial git commit?

1
git update-ref -d HEAD

How do I git rebase the first commit?

1
git rebase -i --root
This post is licensed under CC BY 4.0 by the author.