Skip to content

Instantly share code, notes, and snippets.

@aarongarciah
Last active March 16, 2017 06:59
Show Gist options
  • Save aarongarciah/a63428af0c2736070413 to your computer and use it in GitHub Desktop.
Save aarongarciah/a63428af0c2736070413 to your computer and use it in GitHub Desktop.
Git Tips

See what you’ve been working on today (tweet by @csswizardry)

git log --since="00:00:00" --no-merges --oneline --author=<you@email.com>

git add -p

Rather than git add everything or individual files, this -p will allow you to step through each change, or hunk, and decide if you’d like to commit it. This is really handy if you have made two different changes to the same file and want to commit them separately.

git log -5 --pretty --oneline

View your last 5 latest commits each on their own line.

git shortlog -sn

Quickly get a list of contributors and see how many commits each person has.

git log --all --graph --decorate --oneline --simplify-by-decoration

Pretty branch status in your CLI. You’ll never remember this one so put it in your ~/.gitconfig file under [alias]

git checkout pr/123

Quickly check out a remote for pull request review. You’ll need to set it up like this.

git diff --shortstat "@{0 day ago}"

See how many lines of code you have written today.

git checkout -

It’s like the jump button on your TV remote – jump back to to your last branch.

git reset --soft HEAD~3

A soft reset will keep your changes but allow you to “uncommit” something. Instead of doing a squash, Dan prefers to dial back HEAD any number of commits and then add & commit everything into a single commit.

git reflog

David says it best — “Don’t worry, it’s probably saved somewhere”. Git reflog allows you to see every step you have made with git allowing you to retract and reinstate your steps.

git stash, then git stash pop

git stash will stash those changes and put them into your list — I like to think of this as an array of changes. Now to get them back you can git stash apply but what Sam is suggesting here is that you use git stash pop instead which will also bring back your changes, but it removes them from your stash “array”.

git log -S puppy

Search the commit history for the word puppy and display matching commits.

git latest = for-each-ref --count=30 --sort=-committerdate refs/heads/ --format='%(refname:short)’

Allows you to view your latest branchces – this is helpful for when your branch names are based on bug IDs that you have a hard time remembering.

git config --global help.autocorrect -1

Mistype or misspell a git command? Immediately re-run the correct command. You can use -1 to 1000 to wait a full second before the command is re-run.

git commit --amend

Great for squashing staged files into your last commit.

git cherry-pick [hash]

As long as the commit has been fetched somewhere, you can cherry pick that code in your own branch without having to merge the entire thing.

git remote update --prune

Remove local branches that have been deleted from your remote (like GitHub). You can always run git remote prune origin --dry-run to see what will be deleted before going all in.

git rebase -i HEAD~4

Interactive rebase allows you to pick and choose which commits you can pick, squash, reword, edit, or fixup

1. Parameters fot better logging

git log --oneline --graph

  • --author=“Alex Kras" – Only show commits made by a certain author
  • --name-only – Only show names of files that changed
  • --oneline – Show commit data compressed to one line
  • --graph – Show dependency tree for all commits
  • --reverse – Show commits in reverse order (Oldest commit first)
  • --after – Show all commits that happened after certain date
  • --before – Show all commits that happened before certain data

2. Log actual changes in a file

git log -p lets you view not only the commit message, author, and date, but actual changes that took place in each commit.

git log -p filename

3. Only Log changes for some specific lines in a file

git log -L 1,1:some-file.txt -L flag allows you to specify particular lines in a file that you are interested in. Then Git would only log changes relevant to those lines. It’s kind of like git log -p with focus.

git blame filename to find the person responsible for every line of the file.

4. Log changes not yet merged to the parent branch

git log --no-merges master.. --no-merges flag indicate to only show changes that have not been merged yet to ANY branch, and the master.. option, indicates to only show changes that have not been merged to master branch.

You can also do git show --no-merges master.. or git log -p --no-merges master.. (output is identical) to see actual file changes that are have yet to be merged.

5. Extract a file from another branch

git show some-branch:some-file.js Take a pick at an entire file on a different branch, without switching to this branch.

git show some-branch-name:some-file-name.js > deleteme.js You can also redirect the output to a temporary file, so you can perhaps open it up in a side by side view in your editor of choice.

6. Some notes on rebasing

10. Revert a commit, softly

git revert -n

Regular git revert will automatically re-commit reverted files, prompting you to write a new commit message. The -n flag tells git to take it easy on committing for now, since all we want to do is look.

19. Quickly find a commit that broke your feature (EXTRA AWESOME)

git bisect uses divide and conquer algorithm to find a broken commit among a large number of commits.

Understand what .gitignore rule is ignoring your files git check-ignore -v filename

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