Git tips

How do I discard changes in my working copy that are not in the index?

git stash save --keep-index --include-untracked

You don't need to include –include-untracked if you don't want to be thorough about it.

or drop the stash

git stash drop

or all unstaged files in current working directory use:

git checkout -- .

For a specific file use:

git checkout -- path/to/file/to/revert

Add alias:

Here is what alias I have:

# ~/.gitconfig
[alias]
    squash = "!f(){ git reset --soft HEAD~${1} && git commit --edit -m\"$(git log --format=%B --reverse HEAD..HEAD@{1})\"; };f"
    last = log -1 HEAD
    fb = "!f() { git branch -a | grep ${1} --color=auto; }; f"
    graph = "!f() { git log --graph --date=relative --pretty=tformat:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%an %ad)%Creset'; };f"
    cb = "!f() { git branch -a | grep -m1 -e ${1}.*${2} --color=auto | xargs git checkout; }; f"

git squash

You can add alias by:

$: git config --global alias.squash '!f(){ git reset --soft HEAD~${1} && git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"; };f'

You will the command you add in ~/.gitconfig

squash = "!f(){ git reset --soft HEAD~${1} && git commit --edit -m\"$(git log --format=%B --reverse HEAD..HEAD@{1})\"; };f"

e.g: squash the last 5 commits to 1 commit:

git squash 5

Reference

git find branch

Find the branch whose name includes some keyword

fb = "!f() { git branch -a | grep ${1} --color=auto; }; f"

e.g: find a branch whose name includes certificate:

$: git fb certificate
remotes/origin/bugfix/PGIA-571-fix-certificates

git find branch and checkout

Find the branch name whose name includes some keword:

cb = "!f() { git branch -a | grep -m1 -e ${1}.*${2} --color=auto | xargs git checkout; }; f"

e.g: Find and checkout the branch whose name includes bugfix and 754

$: git cb bugfix 754
Switched to branch 'bugfix/PGIA-754-cannot-close-game'
Your branch is behind 'origin/bugfix/PGIA-754-cannot-close-game' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)
$:(bugfix/PGIA-754-cannot-close-game) ✗

About Tang

A mobile developer, work in Stockholm, Sweden
This entry was posted in git and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s