-
Notifications
You must be signed in to change notification settings - Fork 5
git tips
Shuai YUAN edited this page Mar 11, 2026
·
59 revisions
- Print SHA1 hash:
$ git rev-parse master
$ git rev-parse ca82a6dff817
$ git rev-parse v0.1
ca82a6dff817ec66f44342007202690a93763949git rev-parse --short HEAD$ git cat-file -p$ git ls-files -s$ git write-treegit config --add core.filemode falsegit config --global color.ui autogit config --global core.editor emacs$ git log --follow <file>- Show diffs first for the HEAD version and then the MERGE_HEAD version:
$ git log --merge --left-right -p [<file>]- Search <string></string>:
$ git log -S<string> [--pretty=oneline] [--abbrev-commit] <file>$ git diff master bug/pr-1
# equivalent to
$ git diff master..bug/pr-1$ git diff -S<string>
$ git diff -S"<string>"# rename
git branch -m <newname>- Import an existing git project into GitLab?
- What's the difference between git clone --mirror and git clone --bare
$ git tag <tag name> <tag name>^{} -f -m "<new message>"- Policies:
git commit --file .git/SQUASH_MSGAlterations:
| options | HEAD | index | work_dir | | --soft | Yes | No | No | | --mixed | Yes | Yes | No | | --hard | Yes | Yes | Yes |
$ git cherry-pick <commit># on branch master
$ git cherry-pick A..C
# will add A' B' C' commits on branch master- “git rebase origin” vs.“git rebase origin/master”
- git pull VS git fetch git rebase
- Please, oh please, use git pull --rebase
- http://blog.nutsfactory.net/2010/02/01/git-archive-and-log/
- http://git-scm.com/docs/git-archive
- http://stackoverflow.com/questions/160608/do-a-git-export-like-svn-export
$ git stash pop
# equivalent to
$ git stash apply
$ git stash drop# reapply the staged changes
$ git stash apply --indexe.g.
$ git stash apply --index
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: index.html
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
#
# modified: lib/simplegit.rb
#- `git stash --keep-index`: not stash anything that you’ve already staged with the git add command.
e.g.
$ git status -s
M index.html
M lib/simplegit.rb
$ git stash --keep-index
Saved working directory and index state WIP on master: 1b65b17 added the index file
HEAD is now at 1b65b17 added the index file
$ git status -s
M index.html- `git --include-untracked/-u`: stash the untracked files as well as the tracked ones.
e.g.
$ git status -s
M index.html
M lib/simplegit.rb
?? new-file.txt
$ git stash -u
Saved working directory and index state WIP on master: 1b65b17 added the index file
HEAD is now at 1b65b17 added the index file
$ git status -s
$- Creating a Branch from a Stash:
$ git stash branch <branch-name>$ git stash list --date=local# clean untracked files and directories
$ git clean -fd
# If untracked directory is a git repository of its own (e.g. submodule), you need to use -f twice:
$ git clean -ffd$ git reflog expire --expire=now --all
$ git gc$ git remote -v- Delete a remote branch:
$ git push origin --delete <branch name>
# or
$ git push origin :<branch name>
# if your branch name is the same as some tag name,
# there will be an error like:
# error: dst refspec v2.0 matches more than one.
# Therefore, you have to provide the specified ref like:
$ git push origin :refs/heads/v2.0$ git bisect [start | bad | good]1. Delete the relevant section from the `.gitmodules` file.
2. Stage the `.gitmodules` changes git add `.gitmodules`.
3. Delete the relevant section from `.git/config`.
4. Run `git rm --cached path_to_submodule` (no trailing slash).
5. Run `rm -rf .git/modules/path_to_submodule`.
6. Commit `git commit -m "Removed submodule <name></name>"`.
7. Delete the now untracked submodule files.
8. `rm -rf path_to_submodule`.
# Remove <dir>
git filter-branch --force --index-filter 'git rm -rf --cached --ignore-unmatch <dir>' --prune-empty --tag-name-filter cat -- --all- Delete tmp files
$ git filter-branch --tree-filter 'rm -f *~' -- --all- Obtain a file from another branch
$ git checkout <branch> -- <file name>
# or (in case you want to rename the file)
$ git show <branch>:<path of file> > <new file name>- Overview of what has been changed
$ git whatchanged --since="three days ago" --oneline$ git whatchanged ORIG_HEAD...HEAD- Search
$ git grep- Control file tracing
# [add Makefile changes I do not want published]
$ git update-index --no-assume-unchanged Makefile
$ git add -p Makefile
# [add Makefile changes I want published]
$ git commmit
$ git update-index --assume-unchanged Makefile
$ git push- If you want to commit the changes after deleting lots of files without using `git rm`:
# easier `git` way
$ git add -u
# or more exactly
$ git status | grep deleted | awk '{print $3}' | xargs git rm
# or (from Jonathan Leffler)
$ git status | sed -n '/^# *deleted:/s///p' | xargs git rm
# or (from Vijay C)
$ git rm $(git ls-files --deleted)
# refer to:
# http://stackoverflow.com/questions/6004453/how-to-remove-multiple-deleted-files-in-git-repository
# http://stackoverflow.com/questions/492558/removing-multiple-files-from-a-git-repo-that-have-already-been-deleted-from-disk
# http://stackoverflow.com/questions/6090732/delete-files-from-git-index-when-they-are-already-deleted-from-fs
# http://stackoverflow.com/questions/3169787/remove-all-deleted-files-from-changed-but-not-updated-in-git- Change commit date(修复因系统时间出错而导致的提交时间问题,也可以用来刷Github的streak~XD):
$ git commit --amend --date=<new date>
# e.g.
$ git commit --amend --date="$(date -R)"- Use committer date as author date
git rebase --committer-date-is-author-date- Fix corrupt git index:
# backup maybe...
$ rm -f .git/index
$ git reset
# or using lower level command
$ git read-tree
# if the problem is with index for packfile, recover it using
$ git index-pack- Recover from `git reset`
- * Recover from git reset --hard?
- Move commits to another branch
- * Move the most recent commit(s) to a new branch with Git
# Moving to a new branch
# Note: Any changes not committed will be lost.
git branch newbranch # Create a new branch, saving the desired commits
git reset --hard HEAD~3 # Move master back by 3 commits (GONE from master)
git checkout newbranch # Go to the new branch that still has the desired commits
# Moving to an existing branch
git checkout existingbranch
git merge master
git checkout master
git reset --hard HEAD~3 # Go back 3 commits. You *will* lose uncommitted work.
git checkout existingbranchgit checkout --orphan <branchname>
git rm --cached -r .- Stop tracking remote branches without deleting them
- * How do you stop tracking a remote branch in Git?
git branch -rd $(git branch -a | grep '{pattern}' | cut -d'/' -f2-10 | xargs)- Delete merged branch
- - Git Clean: Delete Already-Merged Branches
- - Cleanup and remove all merged local and remote git branches
- - How can I delete all Git branches which have been merged?
git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d- Generate and apply patches
- - Generate a git patch for a specific commit
git format-patch -1 <sha>
git am < file.patch- backup untracked files
- How to make quick backup of untracked files which I want to delete by git clean?
git ls-files --others --exclude-standard -z | xargs -0 tar rvf ~/backup-untracked.tar- revert `git commit --amend`
- How to undo “git commit --amend” done instead of “git commit”
# Move the current head so that it's pointing at the old commit
# Leave the index intact for redoing the commit.
# HEAD@{1} gives you "the commit that HEAD pointed at before
# it was moved to where it currently points at". Note that this is
# different from HEAD~1, which gives you "the commit that is the
# parent node of the commit that HEAD is currently pointing to."
git reset --soft HEAD@{1}
# commit the current tree using the commit details of the previous
# HEAD commit. (Note that HEAD@{1} is pointing somewhere different from the
# previous command. It's now pointing at the erroneously amended commit.)
git commit -C HEAD@{1}git reset e095 -- somefolder- change author for multiple commits
- How to change the author and committer name and e-mail of multiple commits in Git?
git commit --amend --author "New Author Name <email@address.com>" --no-edit && \
git rebase --continueGIT_SSH_COMMAND="ssh -vvv" git clone example
git -c core.sshCommand="ssh -vvv" pull
GIT_TRACE_PACKET=true git clone ssh://[...]GIT_CURL_VERBOSE=1git branch --contains <commit>
git reflog show --all | grep <commit>git rev-list --ancestry-path 7b4a07a..ecf5891- https://github.com/agis/git-style-guide
- https://www.kernel.org/doc/html/latest/process/submitting-patches.html
- https://gist.github.com/abravalheri/34aeb7b18d61392251a2
- https://github.com/kazupon/git-commit-message-convention
- https://git-scm.com/book/en/v2/Git-Tools-Signing-Your-Work
- Managing OpenPGP Keys
- Creating a new GPG key with subkeys
- OpenPGP Best Practices
- https://stackoverflow.com/questions/38378914/git-error-rpc-failed-curl-56-gnutls
- Rebuilding git with `openssl` instead of `gnutls`
Error in `dpkg-buildpackage`: failed to sign
dpkg-buildpackage -uc -us- https://serverfault.com/questions/191785/how-can-i-properly-sign-a-package-i-modified-and-recompiled
git config --global http.postBuffer 524288000- git clone时出现error: RPC failed; curl 18 transfer closed
- https://stackoverflow.com/questions/38618885/error-rpc-failed-curl-transfer-closed-with-outstanding-read-data-remaining
# specify caching expire
git config --global credential.helper 'cache --timeout 7200'git config credential.helper store`.ssh/config`:
Host githost
HostName git.host.de
Port 4019
User root- Using a remote repository with non-standard port
- window下 配置gitlab ssh非端口22端口
- Configure the /etc/ssh/ssh_config file
bash auto-complete: `Unknown option: --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config`
source /etc/bash_completion.d/git
# or
source /usr/share/bash-completion/completions/git- https://gitlab.com/gitlab-com/support-forum/issues/3508
- https://gitlab.com/gitlab-org/gitlab-ce/issues/21246
- “No newline at end of file” compiler warning
- No Newline at End of File
- No newline at end of file
- How to fix “No newline at end of file” warning for lots of files?
- Why would Vim add a new line at the end of a file?
- what is the meaning of “warning : No new line at end of file”? [duplicate]
- Why should text files end with a newline?
// Skip smudge - We'll download binary files later in a faster batch
git lfs install --skip-smudge
// Do git clone here
git clone ...
// Fetch all the binary files in the new clone
git lfs pull
// Reinstate smudge
git lfs install --force