Skip to content

Instantly share code, notes, and snippets.

@felipevolpatto
Last active July 11, 2017 00:28
Show Gist options
  • Save felipevolpatto/54cc94870ef197f51791 to your computer and use it in GitHub Desktop.
Save felipevolpatto/54cc94870ef197f51791 to your computer and use it in GitHub Desktop.
Git essentials

Remember

Remove local branch
git branch -d (the_local_branch)

Diff files on stating
git diff --staged

Remove a remote branch
git push origin :(the_remote_branch)

Undo local changes (like svn revert)
git checkout foo.txt 

Remove from staging area (local copy still modified).
git reset HEAD foo.txt 

Undo last commit and put changes in staging
git reset --soft HEAD^ foo.txt 

Change last commit
git commit --amend -m "My new commit"

If you have files in staging, they will be used on the new commit.


Shows the difference between two commits
git diff (or difftool) mybranch master -- myfile

The same arguments can be passed to git difftool if you have one configured.


Create a branch that is also set to track the remote branch.
git checkout -t origin/branch-name

Nice log visualization.
[alias]
log1 = log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
log2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n''          %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
lg = !"git log1"

Show log by author and time
git log --author="My name" --after="7 days ago"

'stash unapply'
git stash show -p stash@{0} | git apply -R

Git does not provide such a stash unapply command, but it is possible to achieve the effect by simply retrieving the patch associated with a stash and applying it in reverse.


Lines attributed to each author
git blame --line-porcelain path-file |
sed -n 's/^author //p' |
sort | uniq -c | sort -rn

Count the number of lines attributed to each author

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