Git snippets
These are a view things that i had looked up to solve some problems and i plan to update this post regularly…
To push a new branch to remote
git push origin new_branch |
To delete a remote branch
git push origin :new_branch |
To push new tags
git push --tags origin |
To delete a remote tag
git tag -d 12345 git push origin :refs/tags/12345 |
To reset a local branch to exactly match a remote branch
git fetch origin git reset --hard origin/master |
To abort a rebase
git rebase --abort |
Changing the origin of your git repository (relocate the repository)
git config remote.origin.url [new origin url] |
How do I make git ignore mode changes (chmod)?
git config core.filemode false |
Delete the last commit if it is not pushed yet:
git reset --soft HEAD~1 |
Remove file from repository but not from filesystem (in case you’ve ignored a file but don’t want to delete it)
git rm --cached Foobar.java |
Short log (oneline), including sha and date
git log --pretty=format:"%h %ad%x09%an%x09%s" --date=short |
Count commits by author:
git shortlog -s -n |
Display the first n commits:
git log --pretty=format:"%h %ad%x09%an%x09%s" --date=short --reverse | head -20 |
Merge a branch but don’t commit the merge yet (and avoid fast-forwards):
git merge --no-commit --no-ff theAwesomeFeatureBranch |
List all files changed since commit:
git git diff --name-only COMMIT_ID_OR_WHATEVER_COMMITISH |
This one is useful, if you did a lot of amending to an old commit and want to restore it’s date order for your inner monk:
Do an interactive rebase, edit the commit in question with “e” or “edit”, so that you can amend it again and then continue rebase:
git rebase -i <ref> git commit --amend --reset-author --no-edit git rebase --continue |
Or you could move around the commit during your rebase as well, if it isn’t your last commit.
Group commits by author:
git shortlog -s -n |
Reduce the repositories database size:
git reflog expire --all --expire=now git gc --prune=now --aggressive |
(See this answer)
Pushes a subtree onto a different branch (“dist” being the subtree here):
git subtree push --prefix dist origin gh-pages |
Create a new branch with all the content from the parent but without commits:
git checkout --orphan public |
Last update: 2019/01/06