GIT Cheat Sheet

A Programming › DevOps cheat sheet in English by alexandreceolin

Git useful commands and stuff

4,480 views 1,088 downloads 26 Feb 2024 History

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

.gitconfig

[core]
        excludesfile = ~/.gitignore
        autocrlf = false
        eol = lf
[include]
        path = ~/.gitconfig-local
[fetch]
        prune = true
[push]
        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
[alias]
        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}

Workflow

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

Log

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

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.