git-tricks

neevop 七月 2, 2023


refs

# 1 commit before head
HEAD^

# 2 commits before head
HEAD^^

# 5 commits before head
HEAD-5

branches

# create a new branch
git checkout -b $branch_name

git push origin $branch_name --set-upstream

# get a remote branch
git fetch origin

git checkout --track origin/$branch_name

# delete local remote-tracking branches
git remote prune origin

# list merged branches
git branch -a --merged

# delete remote branch
git push origin :$branch_name

# go back to previous branch
git checkout -

collaboration

# rebase your changes on top of the remote master
git push --rebase upstream master

# squash multiple commits into one for a cleaner git log
git rebase -i $commit_ref

submodules

# import .gitmodules
git submodule init

# clones missing submodules, and checkout commits
git submodule update --init --recursive

# update remote urls in .gitmodules
git submodule sync

diff

# diff from stats
git diff --stat

# just filename
git diff --summary

log options

--online
    e11e9f9 Commit message here
    
--decorate
    shows "(origin/master)"

--graph
    shows graph lines

--date=relative
    "2 hours ago"

misc

cherry pick

git rebase 76acada^

short log

git shortlog

git shortlog HEAD-20..  # last 20 commits

bisect

git bisect start HEAD HEAD-6

git bisect run npm test

git checkout ref/bisect/bad  # this is where it screwed up

git bisect reset

manual bisection

git bisect start
git bisect good  # current version is good

git checkout HEAD-8
npm test  # see if it's good
git bisect bad  # current version is bas

git bisect reset  # abort

searching

git log --grep="fixs things"  # search in commit messages

git log -S"window.alert"  # search in code

git log -G"foo.*"  # search in code (regex)

GPG signing