Skip to content

Instantly share code, notes, and snippets.

@ebarault
Last active February 18, 2019 15:43
Show Gist options
  • Save ebarault/b44a41b49b9bf06af2a48572419c2767 to your computer and use it in GitHub Desktop.
Save ebarault/b44a41b49b9bf06af2a48572419c2767 to your computer and use it in GitHub Desktop.
Git cheatsheet

resync local branch with remote branch

git fetch origin
git reset --hard origin/master
git clean -f -d

delete last commit(s)

# last commit
git reset --hard HEAD^

# last 2 commits
git reset --hard HEAD~2

rework last commit

git reset HEAD^

delete branch

# remove the local branch
git branch -d the_local_branch

# To remove a remote branch (if you know what you are doing!)
git push origin :the_remote_branch

# Or simply use the new syntax (v1.7.0)
git push origin -d the_remote_branch

get latest tag from current branch

git describe --abbrev=0 --candidates=100

remove remote file

git rm --cached mylogfile.log

fetch a single file

git archive --format=tar --remote=git@my.git.host:my-repo.git HEAD:path/to my.file | tar xf -

add/remove tags

# add
git tag 12345
git push --tags

# remove
git tag -d 12345
git push origin :refs/tags/12345

# Or simply use the new syntax (v1.7.0)
git push origin -d 12345

submodules

add submodule

git submodule add -b master https://github.com/project/trackchanges.git path/to/trackchanges

# after cloning the main repo:
git submodule update --init
git submodule update --remote

remove submodule

0. mv a/submodule a/submodule_tmp
1. git submodule deinit -f -- a/submodule    
2. rm -rf .git/modules/a/submodule
3. git rm -f a/submodule
# Note: a/submodule (no trailing slash)

# or, if you want to leave it in your working tree and have done step 0
3.   git rm --cached a/submodule
3bis mv a/submodule_tmp a/submodule

utils

# To count the commits for the branch you are on:
git rev-list --count HEAD

# for a branch
git rev-list --count <branch-name>

# If you want to count the commits on a branch that are made since you created the branch
git rev-list --count HEAD ^<branch-name>

misc

git log --tags --no-walk --pretty="format:%d" | awk '(NR==1){gsub(/tag: |\(|\)/,""); print; exit}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment