Skip to content

Instantly share code, notes, and snippets.

@weshouman
Last active September 22, 2016 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weshouman/0597055da8cd976ebce2091bb966cac8 to your computer and use it in GitHub Desktop.
Save weshouman/0597055da8cd976ebce2091bb966cac8 to your computer and use it in GitHub Desktop.
Git techniques

Setup

Alias git commit to git ci
Alias git status to git st
Alias git diff --cached to git dc

git config --global alias.ci commit
git config --global alias.st status
git config --global alias.dc "diff --cached"

Check the branch and changed files

git st displays those changes

Stage with care

add in patches interactively, hint: be selective
git add -p path
I use s and e options a lot while reviewing the changes for staging

Edit hunks mode

in the e edit mode

  • to cancel staging removed lines marked with '-': replace '-' with ' '
  • to cancel staging new lines marked with '+' either remove or comment them

Note: to comment multiple lines in nano (the default editor) follow this stack overflow answer

Review staged changes

git dc to review the changes for a commit

Reset if needed

If any change wasn't meant to be added, reset it with git reset -p path, that resets in hunks too

Commit

For complete commit messages use
git ci

For inline commit messages (just a header with no body)
Note: that is useful in case multiple comits are following with subtle change in commit message.
git ci -m "Commit message"

Don't use -a on git ci by default!

Fix if anything went wrong

Make your fixes, stage them, then git ci --amend.

Note: Commit message are prombted to be changed after while amending.

Another Note: the commit hash changes based on this step

Push to origin

git push origin branch_name

Caution: If you reached this point, there's no turning back "messing with everybody's history isn't something good"

Stash specific path

update only the modified (tracked files), then reset the specific path

git add -u .
git reset path

Now the path we want to stash is not in index (not staged), let's stash the unstaged changes (the specific path)

git stash save --keep-index "personal notes on commit ######"

now we may check with git st and if anything went wrong undo the stash with git stash pop

Stash specific hunks

This solution is straight forward, but may be slow if the changes are tooooo much and are contained in specific files (in example debugging lines in a big commit)

git stash save -p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment