Skip to content

Instantly share code, notes, and snippets.

@durgaswaroop
Last active February 1, 2018 09:53
Show Gist options
  • Save durgaswaroop/e856a3b467dcc7323c4de00e83828f2f to your computer and use it in GitHub Desktop.
Save durgaswaroop/e856a3b467dcc7323c4de00e83828f2f to your computer and use it in GitHub Desktop.
Useful Git Reference

Interactive Rebase

git rebase -i HEAD~3 // To pick three commits including HEAD. So, you get HEAD, HEAD^, HEAD^^

squash a commit ig you want to combine two commits as a single one.

Revert a file/folder to a commit

git checkout <commit_hash> -- <file/folder name>

Diff current version of file/folder with the version on another commit

git diff <commit_hash> -- <file/folder name>

Amend Commit

If you forgot to add any files to the previous commit, do git add for them and then do amend

git commit --amend

To amend the author name and email,

First do :

git config user.name "User name"

git config user.email "[email protected]"

Then :

git commit --amend --reset-author

Remove unnecessary branches from local repo

git remote update origin --prune

Stashing

git stash save "some message here to remember stash by"

To save untracked files, git stash save -u ....

git stash list - To see the available stashes.

git stash pop - Applies the stash on the current commit and deletes from the stash (By deafult takes the first stash stash{0})

git stash apply - Applies the stash on the current commit but keep the stash for alter use (By deafult takes the first stash stash{0})

git stash drop stash{<Num>} - Deletes that stash completely

git stash show -p stash@{1} - Shows the files in it as a diff

Config Settings

git config --global push.default tracking - Pushes only the current branch and only if tracking is enabled. By default Git pushes all branches that have changes compared to thier remote tracking branches. This makes it safer

To count number of objects in a git directory

git count-objects -v -H

Output contains:

count: the number of loose objects

size: disk space consumed by loose objects, in KiB (unless -H is specified)

in-pack: the number of in-pack objects

size-pack: disk space consumed by the packs, in KiB (unless -H is specified)

prune-packable: the number of loose objects that are also present in the packs. These objects could be pruned using git prune-packed.

garbage: the number of files in object database that are neither valid loose objects nor valid packs

size-garbage: disk space consumed by garbage files, in KiB (unless -H is specified)

Check if a branch contains a commit

git branch --contains <commit> - Lists all branches that have the exact commit

Find a commits that containe a String (searches through entire git history)

git log -S 'search term' --source --all

--source : Shows which branch/stash/tag it picked the commit from

--all: Look in all branches

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment