Collection of Tips and Tricks for using git
- Preview changes before pulling
- Diff a file between two branches
- Undo last commit
- Resetting files
- Reset a branch
- Stash
- Cleanup local repos
- Comparing
- Empty folders
- Push Force safely
- Settings
Start with a git fetch to update the state of the remote branches on your local repo.
git fetch
Show the log entries between your last common commit and the origin's master branch
git log HEAD..origin/master
Show the diffs for each patch
git log -p HEAD..origin/master
Show one liner log If you just run git log, you will notice that the output is quite verbose. If you only want to see the hashes
git log --oneline
Show history of a specific file To check all the changes made on a specific file over time, run
git log --follow file.txt
Show the diffs for a single diff
git diff HEAD...origin/master (n.b. three dots not two)
If you're not prepared to do a pull and merge in all the remote commits, you can cherry pick the specific remote commits you want.
git cherry-pic <commit-ish>
A git pull will merge in the rest of the commits.
git pull
Show all differences of a single file between two branches.
git diff branch1..branch2 -- filename.ext
Undos the last local commit but preserves the changes
git reset --soft HEAD~
Reset all files to the HEAD of the branch
git reset --hard HEAD
Reset a single file:
git checkout HEAD -- path/to/file
Reset already committed changes:
git reset --soft HEAD~1
Reset your local branch to a remote branch
git reset --hard origin/branch_to_overwrite
Important: All tracked local changes will be lost. All local commits that haven't been pushed will be lost. Untracked files will not be affected.
First, run a fetch to update all origin/<branch> refs to latest:
git fetch --all
Backup your current branch:
git branch backup-<branch_name>
Then reset to origin:
git reset --hard origin/<branch_name>
Maintain current local commits It's worth noting that it is possible to maintain current local commits by creating a branch from master before resetting:
git checkout master
git branch new-branch-to-save-current-commits
git fetch --all
git reset --hard origin/master
After this, all of the old commits will be kept in new-branch-to-save-current-commits.
Uncommitted changes will be lost. Make sure to stash and commit anything you need.
git stash
And then to reapply these uncommitted changes:
git stash pop
Works only on tracked files with uncommited changes.
To undo all local changes:
git checkout -- .
To undo changes of a specific file:
git checkout -- <file>
A.K.A. unstage files
git restore --staged .
If you need to cleanup your repo temporarily you have some possibilities to stash your work.
Since git 2.13 you can save a specific file or path to the stash
git stash push -u file.txt
When running git stash, git will include only the tracked files. If you would like to include untracked files you need to add a flag
git stash --include-untracked
git clean -df
Delete all untracked files and directories, including ignored ones:
git clean -d -x -f
Delete a local branch that is completely merged to the server:
git branch -d feature_branch_name
Delete a local branch that is not up to date:
git branch -D feature_branch_name
git fetch --prune origin
Delete stale branches in the local repo that no longer exist in the remote repo in each fetch/pull:
git config --global fetch.prune true
Or manually add the following to your ~/.gitconfig:
[fetch]
prune = true
If you repository slows down and performance is an issue, you can just run the garbage collector
git gc --prune=now
What do you need to know about comparing things in Git?
If you want to know the difference of the content of a file between two branches:
git diff feature_branch main -- file.name
Git only cares about files. Therefore you can't add an empty folder to your repo. As a convention developers started to add a .gitkeep file inside the folder.
touch .gitkeep
Otherwise add a Readme.md file to the folder with information about it.
If you want an empty folder and make sure it stays 'clean' for Git, create a .gitignore containing the following lines within:
# .gitignore sample
# Ignore all files in this dir...
*
# ... except for this one.
!.gitignore
If you desire to have only one type of files being visible to Git, here is an example how to filter everything out, except .gitignore and all .txt files:
# .gitignore to keep just .txt files
# Filter everything...
*
# ... except the .gitignore...
!.gitignore
# ... and all text files.
!*.txt
Forcing a push has several problems especially when you are working in a team. Here's the safe alternative:
git push --force-with-lease
Useful entries for your .gitconfig file
If you're rebasing or cherry-picking a lot and you've ever run into the same conflict more than once, you can turn on a feature where Git memorizes the conflict and the resolution to it. If it ever sees that same conflict again, it will automatically re-solve it for you.
You can turn it on with the config setting rerere.enabled and you can further ask it to automatically stage it for you with rerere.autoUpdate
git config --global rerere.enabled true
git config --global rerere.autoUpdate true
git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"
git config --global --add --bool push.autoSetupRemote true
An example .gitconfig file which can be used for all git repos on the system if it is placed in your home directory.
[user]
name = <UserName>
email = <Email-Address>
[credential]
helper = manager-core
[diff]
tool = bc4
[difftool]
prompt = false
[difftool "vsdiffmerge"]
cmd = \"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Professional\\Common7\\IDE\\CommonExtensions\\Microsoft\\TeamFoundation\\Team Explorer\\vsdiffmerge.exe\" \"$LOCAL\" \"$REMOTE\" //t
keepBackup = false
[difftool "beyondcompare"]
cmd = \"C:\\Program Files\\Beyond Compare 4\\BCompare.exe\" \"$LOCAL\" \"$REMOTE\"
keepBackup = false
[merge]
tool = bc4
[mergetool]
prompt = false
[mergetool "vsdiffmerge"]
cmd = \"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Professional\\Common7\\IDE\\CommonExtensions\\Microsoft\\TeamFoundation\\Team Explorer\\vsdiffmerge.exe\" \"$REMOTE\" \"$LOCAL\" \"$BASE\" \"$MERGED\" //m
keepBackup = false
trustExitCode = true
[mergetool "beyondcompare"]
cmd = \"C:\\Program Files\\Beyond Compare 4\\BCompare.exe\" \"$REMOTE\" \"$LOCAL\" \"$BASE\" \"$MERGED\"
keepBackup = false
trustExitCode = true
[core]
excludesfile = ~/.gitignore
[difftool "bc4"]
path = C:\\Program Files\\Beyond Compare 4\\BCompare.exe
[mergetool "bc4"]
path = C:\\Program Files\\Beyond Compare 4\\BCompare.exe
[credential "https://<credential server url>"]
provider = generic
[core]
excludesfile = ~/.gitignore
git archive --format=zip --output=output.zip
git shortlog -sn
git checkout -