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 file2$ git commit -m 'First Commit'3# Added it again4$ git add last-file5# stage it to the last commit with different commit message6$ 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 --amend2# 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 array2$ git stash apply3# OR4# removes your changes in the stash array5$ 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 --patch23@@ -1,5 +1,5 @@4 ---5-title: 🔥Git Tips & Tricks 🔥6+title: Git Tips & Tricks 🔥7 author: Ahmed Abdulrahman8 date: 2020-01-089 hero: ./images/hero.jpeg10(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 hunkn
No - do not stage this hunkq
quit; do not stage this hunk or any of the remaining onesa
stage this hunk and all later hunks in the filed
No - do not stage this hunk or any of the later hunksg
select a hunk to go to/
search for a hunk matching the given regexK
leave this hunk undecided, see previous hunkj
leave this hunk undecided, see next undecided hunkJ
leave this hunk undecided, see next hunke
manually edit the current hunks
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
: AddedC
: CopiedD
: DeletedM
: ModifiedR
: RenamedT
: Have their type changedU
: UnmergedX
: UnknownB
: Have their pairing broken*
: All or none
To Include
use Upper-case
1# Show only modified files2$ git diff --diff-filter=M3# Show only Modified files over the previous4# 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 -sn2252 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:%B34# --no-merges ─ Show the whole commit history, but skip any merges5# --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!