GIT_TRACE=true git pull
# may need to remove dangling commits and blobs:
git fsck && git gc --prune=now
# also try deleting stale connections in ~/.ssh
https://stackoverflow.com/questions/44031150/why-does-git-pull-hang
git gc --prune=now
cp -a .git .git.bak
find .git/objects/ -type f -empty | xargs rm
git fetch -p
git fsck --full
https://stackoverflow.com/questions/11706215/how-to-fix-git-error-object-file-is-empty
git lfs uninstall
git reset --hard
git lfs install
git lfs pull
https://github.com/git-lfs/git-lfs
# this modifies .gitattributes file
git lfs track "*.psd"
# then just use git add, git commit
# make sure you also commit changes to .gitattributes!
# to untrack
git lfs untrack foo.psd
# clone repo / pull without large files
# one-time
GIT_LFS_SKIP_SMUDGE=1 git clone SERVER-REPOSITORY
# always
git config --global filter.lfs.smudge "git-lfs smudge --skip"
https://stackoverflow.com/questions/42019529/how-to-clone-pull-a-git-repository-ignoring-lfs
git rebase -i <commit>^ # 1 before the commit
git reset HEAD^
# replace "pick" with "edit"
# commit pieces individually in usual way
git rebase --continue
https://stackoverflow.com/questions/6217156/break-a-previous-commit-into-multiple-commits
git rebase -i --root
https://stackoverflow.com/questions/2246208/change-first-commit-of-project-with-git
git config --global core.editor "vi"
# in one command
git submodule update --init --recursive
# separately
git submodule init
git submodule update
https://git-scm.com/book/en/v2/Git-Tools-Submodules https://stackoverflow.com/questions/3796927/how-to-git-clone-including-submodules
Example: horovod uses them: https://github.com/horovod/horovod/blob/master/.gitmodules
git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -10 | awk '{print$1}')"
git merge-base <branch1> <branch2>
http://stackoverflow.com/questions/1549146/find-common-ancestor-of-two-git-branches
git branch | tr -d \* | xargs git grep "foo"
# order matters as rules are processed sequentially
*.user # ignore all paths ending in '.user'
!*.user/ # but don't ignore these paths if they are directories.
https://stackoverflow.com/questions/16770282/how-to-gitignore-only-files
/file
https://stackoverflow.com/questions/3637660/how-to-exclude-file-only-from-root-folder-in-git
git config core.ignorecase false
# alternative approach
git mv File file.tmp
git mv file.tmp file
# Git aliases
alias ga="git add"
alias gb="git branch"
alias gca="git commit --amend"
alias gce="git commit --allow-empty -m"
alias gcfd="git clean -fd"
alias gcm="git commit -am"
alias gco="git checkout"
alias gcp="git cherry-pick"
alias gcr2="git commit -am will-be-deleted && git rebase -i HEAD~2"
alias gcr2p="git commit -am will-be-deleted && git rebase -i HEAD~2 && rbt post -g no -r"
alias gd="git diff"
alias gdesc="git describe"
alias glg="git log"
alias gpl="git pull"
alias gp="git push"
alias gr="git rebase"
alias grs="git reset"
alias grsh1="git reset --hard HEAD~1"
alias grd="git review dcommit -r"
alias gsh="git show"
alias gst="git status"
alias gt="git tag"
git update-index --skip-worktree <file_name>
# to track local changes again, use
git update-index --no-skip-worktree <file_name>
# list flagged files
git ls-files -v | grep '^S'
https://compiledsuccessfully.dev/git-skip-worktree/
git --work-tree=~/git --git-dir=~/git/.git diff -- git.c
http://stackoverflow.com/questions/8560618/how-can-i-use-full-paths-in-git
git remote add upstream git://github.com/user/repo.git
git remote set-url <remote> git://...
git reset --hard HEAD
git pull upstream master // git pull latest from upstream master
git pull <remote> <src>:<dest>
git push <remote> <local branch>:<remote branch>
git push upstream junit-runner:master
git push origin initial-queries:initial-queries
git branch --set-upstream <local-branch> <remote>/<remote-branch>
git branch -vv
git push <remote> <branch>
git fetch <remote>
git fetch <remote> <local-branch>:<remote-branch>
http://stackoverflow.com/questions/11266478/git-add-remote-branch
git fetch --unshallow
# assuming you have less than 1 million commits
git fetch --depth=1000000
https://stackoverflow.com/questions/6802145/how-to-convert-a-git-shallow-clone-to-a-full-clone
You might need to do this if you did a shallow clone originally.
git fetch origin +branch-1:branch-1
git fetch --tags
A git tag refers to a specific commit and does not change.
# delete remote branch
git push origin :<tagname>
# update commit local tag points to
git tag -fa <tagname>
# push tag
git push origin <tagname>
Annotated tags are meant for release versions.
git tag -a TAG_NAME
git tag --sort version:refname
git push origin :tagname
http://stackoverflow.com/questions/5480258/how-to-delete-a-remote-git-tag
git tag
# With one-line description
git tag -n1
git tag -l 1.2.3 -n9
git describe
git rev-list $TAG | head -n 1
# also consider using
git log --one-line
# to quickly scan past commits and any tags associated with them
http://stackoverflow.com/questions/1862423/how-to-tell-which-commit-a-tag-points-to-in-git
git rev-list TAG..HEAD --count
# Push one tag
git push origin <tag>
# Push all tags
git push --tags
git checkout <branch> <file>
https://stackoverflow.com/questions/2364147/how-to-get-just-one-file-from-another-branch/2364223
git checkout -b li-0.10-1 origin/li-0.10-1
git push origin --delete initial-queries
git remote prune <remote>
https://stackoverflow.com/a/4969679/1128392
# will fast forward by default
git merge <branch> # merges <branch> into current branch
# Use branch that you're merging or rebasing
git checkout --theirs -- path/to/conflicted-file.txt
# Use your version of the file
git checkout --ours -- path/to/conflicted-file.txt
# http://stackoverflow.com/questions/278081/resolving-a-git-conflict-with-binary-files
git checkout HEAD~5 # checkout 5 commits ago
git checkout COMMIT_HASH # checkout at specific commit
git checkout <branch>
git clean -f [-d]
# -d removes untracked directories, too
# -i for interactive
# -x do NOT use standard ignore rules from .gitignore and $GIT_DIR/info/exclude, allows for removing all untracked files including build products
git clean -idx
git add -A
git reset HEAD^ path/to/file
git reset --soft HEAD^
git show
git show <commit>:<file>
git blame <commit> -- <file>
http://stackoverflow.com/questions/5098256/git-blame-prior-commits
# modify author
git commit --amend --author="Author Name <[email protected]>"
# https://stackoverflow.com/questions/3042437/change-commit-author-at-one-specific-commit
# Changes the most recent commit message
git commit --amend -m "New commit message"
# Change up to older commit
git rebase --interactive 'bbc643cd^'
# Change "pick" to "e" or "edit"
http://stackoverflow.com/questions/1186535/how-to-modify-a-specified-commit-in-git
git commit --allow-empty
# stash one file
git stash push path/to/file
git stash save "name"
git stash apply stash^{/name} # Doesn't seem to work anymore
git stash apply stash@{0} # where 0 is a number (do git stash list first)
git pull --rebase
git branch -m <newname>
git branch -m old_branch_name new_branch_name
git rebase -i HEAD~2 # reorder last two commits
git rebase <branch> # replays <branch> on your current branch
git rebase <remote>/<branch>
git rebase -i COMMIT_HASH
http://stackoverflow.com/questions/134882/undoing-a-git-rebase
git reflog
git reset --hard HEAD@{<number>}
Try changing URL from HTTPS to SSH. Try including/excluding https:// or ssh://. Try adding or removing USERNAME@. Try using git@.
git diff tag1 tag2
https://stackoverflow.com/questions/3211809/how-to-compare-two-tags
cd first-repo
git diff HEAD^ -- hello.test > ~/patch_file
cd ../second-repo
patch -p1 blue/red/hi.test ~/patch_file
# 200 is the line width
git show <commit> --stat=200
https://stackoverflow.com/questions/10459374/making-git-diff-stat-show-full-file-path
# diff with parent
git diff 0ac424f0a17b341efe299da167791112e4a953e9^!
# http://stackoverflow.com/questions/17563726/how-to-see-the-changes-in-a-commit
# generate diff of one file in commit
git diff <commit>^! -- path/to/file > foo.patch
# apply patch to different path
# -p<n> removes `n` leading directories
# --directory prepends a path
# `-3` allows conflict resolution
cat foo.patch | git apply -3 -p2 --directory='new/path/'
# one-line
git diff <commit>^! -- path/to/file | git apply -3 -p2 --directory='new/path/'
# -p1 strips off a/ and b/ prefixes Git automatically adds
patch -p1 new/path foo.patch
# git: comparing a file between two branches
git diff BRANCH1 BRANCH2 FILE
# git diff file that has been moved
git diff 6fab939e28486ab6e6d5038f33f5e9c3b406d4e1:ql/src/java/org/apache/hadoop/hive/ql/exec/MapRedTask.java ql/src/java/org/apache/hadoop/hive/ql/exec/mr/MapRedTask.java
# git diff one file
git diff <commit1> <commit2> <file>
git diff HEAD~1 HEAD <file>
# git diff, only show files changed
git diff --stat f57ea64
# git binary diff
git diff --binary
# Treat all files as text
git diff --text
# Highlight whitespace changes
# Often easiest just to do reverse diff
git diff -R
# https://stackoverflow.com/questions/5257553/coloring-white-space-in-git-diffs-output
# diff while ignoring whitespace changes
git diff -w
# diff two branches
git diff branch1..branch2
# http://stackoverflow.com/questions/9834689/comparing-two-branches-in-git
git diff path/to/file > changes.patch
patch -p1 path/to/other/file changes.patch
# repo 1
# add --binary if binary change
git diff FILE > patch
# repo 2
git apply /path/to/patch
git cherry-pick -n <commit> # get your patch, but don't commit (-n = --no-commit)
git reset # unstage the changes from the cherry-picked commit
git add -p # make all your choices (add the changes you do want)
git commit # make the commit!
https://stackoverflow.com/questions/1526044/partly-cherry-picking-a-commit-with-git
# use `git apply -3 -` to allow for conflict resolution
# can also use `-p<n>` and `--directory`
git show SHA -- file1.txt file2.txt | git apply -
http://stackoverflow.com/questions/5717026/how-to-git-cherry-pick-only-changes-to-certain-files
git apply
also support --include=<path-pattern>
to only apply patches to some files.
git cherry-pick HASH
git pull <remote> <branch>
# to pull from another local branch, push it to remote and then pull from remote
# repo 1
git show <commit> > foo.patch
# repo 2
git apply /path/to/foo.patch
# Alternative
/path/to/1 $ git format-patch sha1^..sha1
/path/to/1 $ cd /path/to/2
/path/to/2 $ git am /path/to/1/0001-…-….patch
git apply --stat fix.patch # see patch changes
git apply --check fix.patch # dry-run
git apply fix.patch # apply for real
https://ariejan.net/2009/10/26/how-to-create-and-apply-a-patch-with-git/
git checkout <branch> -- <filename>
# Add --no-edit to avoid edit prompt
git revert <commit_hash>
http://stackoverflow.com/questions/2733873/reverting-a-single-file-to-a-previous-version-in-git
git checkout <commit> path/to/file
http://stackoverflow.com/questions/4795600/reverting-part-of-a-commit-with-git
$ git revert -n $bad_commit # Revert the commit, but don't commit the changes; -n means --no-commit
$ git reset HEAD . # Unstage the changes
$ git add --patch . # Add whatever changes you want
$ git commit # Commit those changes
http://stackoverflow.com/questions/1657017/squash-all-git-commits-into-a-single-commit
rm -rf .git
git init
git add .
git commit -m "<commit message>"
# Squash last 3 commits
git reset --soft HEAD~3
git commit -m 'New commit message'
http://stackoverflow.com/questions/5189560/squash-my-last-x-commits-together-using-git
http://stackoverflow.com/questions/278192/view-the-change-history-of-a-file-using-git-versioning
git log -- [file]
# Tracks files through git mv's
# http://stackoverflow.com/questions/3845234/viewing-git-history-of-moved-files
git log --follow -- [file]
git log [FOLDER]
http://stackoverflow.com/questions/11950037/view-git-history-for-folder
git log -i -S word
git log -i --grep=word
git log -1 --pretty=format:%s
git log --graph
http://stackoverflow.com/questions/1838873/visualizing-branch-topology-in-git
git reflog git reset
http://stackoverflow.com/questions/3689838/difference-between-head-working-tree-index-in-git
HEAD^
is short forHEAD^1
and means the first parent ofHEAD
.HEAD~n
means to go back n commits fromHEAD
, favoring the first parent in cases of ambiguity- http://stackoverflow.com/questions/1955985/what-does-the-caret-character-mean
- To go back 1 commit on the second parent, use
HEAD^2
.
On Windows, git looks for ~/.ssh/id_rsa
to use as your SSH key
After installing the side-by-side diff tool "meld", you can do
git difftool <commit_hash1>..<commit_hash2>
However, this compares files one by one. It's often easier to fit to a
git reset --soft HEAD~<number>
and then do a
meld .
which will open all the files at once
git rm --cached <files...>
When cloning with one URL fails, try another one (SSH, HTTP, Git)
git config --global color.ui true
http://stackoverflow.com/questions/1156069/how-to-configure-mac-os-x-term-so-that-git-has-color
git config --global core.pager more
http://michael.otacoo.com/linux-2/avoid-escape-characters-in-git/
# one config
git config --get <CONFIG_NAME>
# all configs
git config -l
# Push only current branch
git config --global push.default simple
# Push all matching branches
git config --global push.default matching
git config --global user.email "<email>"
git commit --author="John Smith <[email protected]>"
git clone --depth 1 ...
git clone <repo> <new_name>
git clone --depth DEPTH --branch BRANCH GIT_URI
http://strk.keybit.net/blog/2011/06/07/getting-just-the-tip-of-a-remote-git-branch/
# Not directly possible, so you have to use `git reset`
git clone $URL
git reset --hard $SHA1
git clone [email protected]:whatever folder-name
http://stackoverflow.com/questions/651038/how-do-you-clone-a-git-repository-into-a-specific-folder
git ls-files FILE --error-unmatch
git log -S <string> path/to/file
http://stackoverflow.com/questions/4404444/how-do-i-blame-a-deleted-line
git show <revision>:path/to/file
In iTerm2:
git show <commit>
Cmd + click file name (--- a/path/to/file)
git log --all -- <path-to-file>
git log --all -- **/thefile.*
ssh-add -l
ssh-keygen -lf ahsu_ssh_key.pub
eval $(ssh-agent)
ssh-add PRIVATE_KEY_FILE
http://stackoverflow.com/questions/10032461/git-keeps-asking-me-for-my-ssh-key-passphrase
If you're SSHing to a remote host and want your ssh-agent forwarded, use
ssh -A ...
http://superuser.com/questions/141044/sharing-the-same-ssh-agent-among-multiple-login-sessions