Navigate back to the homepage

Git Tips & Tricks 🔥

Learn how to improve your Git-based workflow with cool tips & tricks that can save you some time. It's time to clean up your command line!

Ahmed Abdulrahman
Jan 8th, 2020 • ☕️ 3 min read

Photo by David Klein on Unsplash

Here’s a series of cool tips & tricks like stashing, loggin, and write git aliases to save you some time while you are working.

git reset

Whether you are working on a personal or team project, it is quite common to create branches, add files and stage them. However, in some cases, you might realize the changes you made were not what you wanted but you want to revert the changes back. Here git reset come into play, its a powerful technique every developer need to know ⚡️. git reset comes with three modes:

  • git reset --soft HEAD~2: undo last two commits, but keep the changes contained in that commit are not lost , they are in our Staging Area and Working Directory.
  • git reset --hard HEAD~2: undo last two commits, but the changes contained in that commit are now neither in our Working Directory or the Staging Area, so it completely removes all changes.
  • git reset -- mixed HEAD~2: undo last two commits, but the changes from that commit are (only) in our Working Directory.

git checkout -

It allows you to switches back to the branch you were on previously.

git commit —amend

It allows you to modify your last commit. Let’s say right after making staging your changes, you find out you forgot to stage a file for that commit and you need stage it but reword the commit message. To make the correction you run:

1# Staged all files but forgot a file
2$ git commit -m 'First Commit'
3# Added it again
4$ git add last-file
5# stage it to the last commit with different commit message
6$ git commit --amend

It will open up your visual editor of choice with the last commit message allowing you to modify it, change the message, save it and exit. After saving it:

  • A new commit will be created.
  • Replace the commit with the previous message.

But that’s not all it do, you can fix any typos in commit messages:

1$ git commit --amend
2# or you can set the new commit message directly without opening editor:
3$ git commit --amend -m "New message"

git stash

It’s called panic button 🚨 and let’s you to store unstaged changes in the working directory away, focus on an emergency bugfix or similar, and later revisit your stashed code then apply those changes back on 🤙. Just run:

1$ git stash

and your working directory reverts to what it’s last commit but this won’t store files not yet tracked by git. To solve this we can pass -u flag (include untracked) to the stash command:

1$ git stash -u

Want your changes back?

1# keeps your changes in the stash array
2$ git stash apply
3# OR
4# removes your changes in the stash array
5$ git stash pop

Want to view all stored stashes you made?

1$ git stash list

git add —patch or -p

It allwos you to select subset of a file’s changes instead of immediately adding all the changes in the file, and then you can commit only the part you want. So, if you pass p to git add it will look like this

1$ git add --patch
3@@ -1,5 +1,5 @@
4 ---
5-title: 🔥Git Tips & Tricks 🔥
6+title: Git Tips & Tricks 🔥
7 author: Ahmed Abdulrahman
8 date: 2020-01-08
9 hero: ./images/hero.jpeg
10(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?

below you have all available options. Pressing ? gives you explanation of each option:

  • y Yes - stage this hunk
  • n No - do not stage this hunk
  • q quit; do not stage this hunk or any of the remaining ones
  • a stage this hunk and all later hunks in the file
  • d No - do not stage this hunk or any of the later hunks
  • g select a hunk to go to
  • / search for a hunk matching the given regex
  • K leave this hunk undecided, see previous hunk
  • j leave this hunk undecided, see next undecided hunk
  • J leave this hunk undecided, see next hunk
  • e manually edit the current hunk
  • s split the current hunk into smaller hunks (this only works if there’s unchanged lines between the changes in the displayed hunk)
  • ? print help

This is really handy it lets you write as much code as you want to without having to remember to commit each change separately beforehand or if you have made several different changes to the same file and want to commit them separately.

git checkout —patch or -p

This is like git add --patch command, but it puts you on an intractive workflow where you can decide how to handle each chunk of your changes. I personally find it a great way to go through before trying to commit your changes.

git diff —diff-filter

Diff command is used when we want to track difference between any changes made on a file. It gets a big task when working with file cleanup, removing and modifing lots of files.

To filter all noises and filter by type of file change Added, Modified, or Updated, we can pass --diff-filter flag together with any of the following characters to git diff:

  • A: Added
  • C: Copied
  • D: Deleted
  • M: Modified
  • R: Renamed
  • T: Have their type changed
  • U: Unmerged
  • X: Unknown
  • B: Have their pairing broken
  • *: All or none

To Include use Upper-case

1# Show only modified files
2$ git diff --diff-filter=M
3# Show only Modified files over the previous
4# five commits and output changes to file.
5$ git diff --diff-filter=M HEAD~5 > changes.txt:

To Exclude use Upper-case

1# Show all but not modified files.
2$ git diff --diff-filter=m

Any combination of characters can be used 🔥

git shortlog -sn

It lets you list all contributors and see how many commits each person has made on the repo.

1$ git shortlog -sn
2252 Ahmed Abdulrahman

git log —after & —before

Let’s you trace back all commits to find specific commit between a given date range. I use it mostfy for time reporting, just run:

1$ git log --no-merges \
2 --after="2020-01-01" --before="2020-02-29" --pretty=format:%B
4# --no-merges ─ Show the whole commit history, but skip any merges
5# --after ─ Show commits more recent than a specific date.
6# --before ─ Show commits older than a specific date.
7# --pretty ─ Pretty-print the contents of the commit logs in a given format

git remote update —prune

Both git remote update remote is same as git fetch remote, they fetch updates for remotes or remote groups in the repository as defined by remotes group. With --prune, it process simply removes local branches that have been deleted from your remote (like GitHub).

Did you find this useful? Buy me a coffee to give my brain a hug☕️.

Hope you liked this article. If you did, then share it. It means a lot.🙌 Thanks for reading!

Discuss on TwitterFollow @_ahmed_ab

Other things I've written

Copy highlighted code snippets to the clipboard in JavaScript

Allowing users to copy Code Snippets to clipboard is a popular convenience feature for Websites. In this article, I will show you how to add Copy to Clipboard functionality to your Web Apps

Jan 3rd, 2020 · 1 min read

Useful utilities functions to use in your next JavaScript project

a collection of useful functions that you can use to create or expand your utilities library!

Jan 2nd, 2020 · 1 min read

© 2018–2020
Ahmed Abdulrahman


Link to $ to $ to $