svenbergner / git-cheatsheet

Collection of tips and tricks for using git

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Git CheatSheet

Collection of Tips and Tricks for using git

Contents

Preview changes before pulling

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

Diff a file between two branches

Show all differences of a single file between two branches.

    git diff branch1..branch2 -- filename.ext

Undo last commit

Undos the last local commit but preserves the changes

    git reset --soft HEAD~

Resetting files

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 a branch

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    

Undo local changes

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>

Restore the Staged Area

A.K.A. unstage files

    git restore --staged .

Stash

If you need to cleanup your repo temporarily you have some possibilities to stash your work.

Create Stash with a Single File/Path

Since git 2.13 you can save a specific file or path to the stash

    git stash push -u file.txt

Create Stash including untracked files

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

Cleanup local repos

Clean All (tracked and untracked files)

    git clean -df

Remove untracked files

Delete all untracked files and directories, including ignored ones:

    git clean -d -x -f

Delete local branches

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

Manually Delete local branches that have been removed from remote repo on fetch or pull

    git fetch --prune origin

Always delete local branches that have been removed from remote repo on fetch or pull

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

Optimize git repository performance

If you repository slows down and performance is an issue, you can just run the garbage collector

    git gc --prune=now

Comparing

What do you need to know about comparing things in Git?

Comparing a file from two different branches

If you want to know the difference of the content of a file between two branches:

    git diff feature_branch main -- file.name

Empty folders

Adding an empty folders to your repo

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.

Keep a folder empty

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

Push Force safely

Forcing a push has several problems especially when you are working in a team. Here's the safe alternative:

    git push --force-with-lease

Settings

Useful entries for your .gitconfig file

Reuse Recorded Resolution

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

Using Windows Git Credential Manager on WSL

    git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"

Automatically setup remote branch on pushing

    git config --global --add --bool push.autoSetupRemote true

Global .gitconfig

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

Using a global .gitignore file

    [core]
        excludesfile = ~/.gitignore

Backup repository on a zip file

    git archive --format=zip --output=output.zip

Show top contributors

    git shortlog -sn

Move back to the previous branch

    git checkout -

About

Collection of tips and tricks for using git

License:Creative Commons Zero v1.0 Universal