Show Menu

Git useful commands and stuff

Useful Commands

Which commit does this branch come from?
git merge-base new_branch old_branch
Which merged branches were not deleted?
for branch in `git branch -r --merged origin­/master | grep -v HEAD`;do echo -e `git show --form­at=­"%ai %ar by %an" $branch | head -n 1` \\t $branch; done | sort -r
How to amend a (non-p­ushed) commit?
git commit --amend --no-edit
How to clear local tags
git fetch --prune origin "­+re­fs/­tag­s/*­:re­fs/­tag­s/*­"
How to display inform­ation about origin?
git remote show origin
How to setup the upstream of branch?
git checkout --track origin­/br­anc­hname
How to reset local branch to remote's HEAD?
git reset --hard origin
How to check out more than one branch at a time?
git worktree


        excludesfile = ~/.gitignore
        autocrlf = false
        eol = lf
        path = ~/.gitconfig-local
        prune = true
        autoSetupRemote = true
        default = upstream
[color "branch"]
        current = yellow reverse
        local = yellow
        remote = green
[color "diff"]
        meta = yellow bold
        frag = magenta bold
        old = red bold
        new = green bold
[color "status"]
        added = yellow
        changed = green
        untracked = cyan
        remoteBranch = cyan
        lg = log --graph --oneline
        hist = lg --decorate --all
        st = status -sb
        co = checkout
        br = branch
        pushf = push --force-with-lease
        ver = describe --tags
        fuckup = commit --fixup HEAD
        praise = blame

diff & stash

How to include stage in diff?
git diff --staged
How to diff words instead of lines?
git diff --word­-diff
What has changed in a file?
git diff HEAD~1..HEAD file.txt
How to diff a file with main?
git diff HEAD..main file.txt
How to diff a stash with the commit it's based on?
git diff stash@­{0}^!
How to see the most recent stash?
git stash show -p stash@{0}


git fetch
git checkout feature/feature-x
git commit -m "Add feature x"
git push
git checkout develop
git pull
git checkout feature/feature-x
git rebase develop
git add fixed files list
git rebase --continue
git checkout develop
git merge --no-ff feature/feature-x
git push
git branch -D feature/feature-x
git push origin :feature/feature-x


git log --oneline
git log --stat
git log --name­-status
git log --follow --pret­ty=­for­mat­:'%an (%ae)' file.c | sort | uniq


No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Git Commands Cheat Sheet
          Shell utilities & sytem management Cheat Sheet
          Terminal - français Cheat Sheet

          More Cheat Sheets by alexandreceolin

          tmux - terminal multiplexer Keyboard Shortcuts