Useful Git Snippets

May 7, 2023 note-to-self

Bring back a deleted file, after it's been committed and pushed:

git checkout <deletion commit hash>~1 -- <filename>

Get the most recent log entry:

git log -1

Compare:

https://github.com/ha17/foobar/compare/<sha or branch or tag>...<sha or branch or tag>

Pruning local branches no longer tracking a remote branch

git remote prune origin # <<<< removes any REMOTE branches, locally
git branch --v | grep "\[gone\]" | awk '{print $1}'| xargs git branch -d

Pruning local branches no longer on the remote:

git fetch --prune
# or
git remote prune origin

See everything you did

git reflog --date=iso

Undo a commit, but leave committed changes as modified files

git reset <commit hash>

Undo a commit, but leave committed changes as staged changes

git reset --soft <commit hash>

Undo a commit, but delete the changes in the commit

git reset --hard <commit hash>

Undo a commit, go back one

git reset HEAD~1

SSL Issues

Pull without going thru all the SSL self-signed BS but also not permanently overriding it. I used this a few times on a client network with a self-signed cert in the chain but no time to update the chain:

git -c http.sslVerify=false pull

Another way: GIT_SSL_NO_VERIFY=true git push --tags (--tags is optional, I needed it for that)

Search all branches

git log --all --grep "<search>"

Search current branch

git log --grep "<search>"

See everything since a date

git log --since="2022-04-01"

See all the commits that affect

git log --follow -- <filename>

search/grep all commits/changes (NOT log)

git grep "<search>" $(git rev-list --all)

Merging with theirs/ours

git merge -X ours/theirs <branch>

Look for conflict markers

git diff --check

The diff of everything that is currently staged

git diff --cached

Finding all the branches that have a commit

git branch [-r|-a] --contains <commit>

Find only merge commits

git log --merges

Find only merge commits starting with

git log --merges <commit>..

Find only non-merge commits

git log --no-merges

Not merged -- find any branches not merged into the current branch (amazing!)

git branch --no-merged

Merged -- lists branches merged into main

git branch --merged main

Merged -- lists branches merged into HEAD (i.e. tip of current branch)

git branch --merged

Shows details about what branches are tracking, whether they are updated, etc (amazing!)

git remote show origin

List branches, including tracking info

git branch [-r|-a] -vv

Turn off case sensitivity via git (per repo)

git config core.ignorecase false
Most posts are for my own reference and reflection, and shouldn’t be taken as fully accurate or instructional.