Git snippets

December 22, 2011 by Michael

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

No comments yet

Post a Comment

Your email is never published. We need your name and email address only for verifying a legitimate comment. For more information, a copy of your saved data or a request to delete any data under this address, please send a short notice to michael@simons.ac from the address you used to comment on this entry.
By entering and submitting a comment, wether with or without name or email address, you'll agree that all data you have entered including your IP address will be checked and stored for a limited time by Automattic Inc., 60 29th Street #343, San Francisco, CA 94110-4929, USA. only for the purpose of avoiding spam. You can deny further storage of your data by sending an email to support@wordpress.com, with subject “Deletion of Data stored by Akismet”.
Required fields are marked *