AK DevCraft
Posted on September 11, 2021
This post is in continuous of Git basic: essential commands - Part 1 post, if you've not read it, kindly read it.
Here we will see a list of basic Git essential commands that are very handy.
9. Find out the difference between two commits
git diff older-commit-hashvalue..newer-commit-hashvalue
E.g.
Get the hash/SHA1 value of the commits you want to see difference
ak@--mac git-useful-commands % git log -n8
commit 8ed86ffedde17486f00b5abee13414018ac96642 (HEAD -> main)
Author: AK <email@address.com>
Date: Fri Sep 10 22:47:33 2021 -0500
Update second file
.
.
.
.
commit 9987cb5abc772a4b2a13ca741fd56e1a5f3c0a1d
Author: AK <email@address.com>
Date: Fri Sep 10 21:28:34 2021 -0500
Create new directory for git useful commands
9987cb5abc772a4b2a13ca741fd56e1a5f3c0a1d is the hash/SHA1 value, to see details first few hex chars are enough, I’ll be using first 7 chars
ak@--mac git-useful-commands % git diff 9987cb5..8ed86ff
--Output--
diff --git a/git-useful-commands/second-file.txt b/git-useful-commands/second-file.txt
index 75d6370..0a19e48 100644
--- a/git-useful-commands/second-file.txt
+++ b/git-useful-commands/second-file.txt
@@ -1 +1,3 @@
my second file
+
+Modify second file
10. Undo file in the working directory
It overrides the file with the last commit version of the file
git checkout -- filename
Or
git restore filename
E.g.
ak@--mac git-useful-commands % git checkout -- second-file.txt
ak@--mac git-useful-commands % git restore second-file.txt
11. Undo file in the staging index
git restore --staged filename
E.g.
ak@--mac git-useful-commands % git restore --staged second-file.txt
12. Amend last commit message
It updates the last commit message, helpful in fixing a typo in the commit message or in case you want to just update the commit message.
git commit --amend -m "Commit Message"
E.g.
ak@--mac git-useful-commands % git commit --amend -m "Update second file"
12. Amend last commit file set
First, edit the required file and stage it by the git add filename
command. We can use the git commit
amend option to add a new file in the last commit file set. And optionally update commit message as well.
E.g.
ak@--mac git-useful-commands % git add second-file.txt
ak@--mac git-useful-commands % git commit --amend -m "Update second file"
13. Check-out a previous version file
May be we want to go back to couple of versions and make the change on top of it. This command checkout the file version and override the file in the working directory.
git checkout hashvalue -- filename
E.g.
Get hash/SHA1 value of the commit for the file version you want
ak@--mac git-useful-commands % git checkout 9987cb5ab -- second-file.txt
14. Revert commit
It creates a new commit on top of the HEAD and revert all changes done in the file set. It may rise to merge conflict in case you're going back to multiple versions and the file had changes in all of the previous versions.
git revert hashvalue
E.g.
Get hash/SHA1 value of the commit you want to revert
ak@--mac git-useful-commands % git revert 4743e81de
15. Remove untracked file from the working directory
git clean -f
E.g.
Option n
is the dry run and displays what files may be deleted. And option f
will delete the file. Don't get confused git clean -f
with git rm
, as later command delete the tracked file or in other words existing file.
ak@--mac git-useful-commands % git clean -n
Would remove temp1.txt
Would remove temp2.txt
ak@--mac git-useful-commands % git clean -f
Removing temp1.txt
Removing temp2.txt
16. Track empty directory
E.g.
When we create an empty directory, Git won't track it by default, as it tracks files, not the directory.
ak@--mac git-useful-commands % mkdir temp
ak@--mac git-useful-commands % git st
On branch main
Your branch is ahead of 'origin/main' by 3 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
Now add a hidden file, the general convention is to create .gitkeep
file.
ak@--mac git-useful-commands % touch temp/.gitkeep
ak@--mac git-useful-commands % git st
On branch main
Your branch is ahead of 'origin/main' by 3 commits.
(use "git push" to publish your local commits)
Untracked files:
(use "git add <file>..." to include in what will be committed)
temp/
Later stage and commit
ak@--mac git-useful-commands % git add temp/.gitkeep
ak@--mac git-useful-commands % git commit -m "Add temp dir"
17. Directly commit the tracked file
git commit -am filename
E.g.
git commit -am first-file.txt
WARNING - We cannot directly commit a new file, it only works with an existing file. For a new file, stage it and then commit.
If you have reached here, then I did a satisfactory effort to keep you reading. Please be kind to leave any comments or ask for any corrections. Happy Coding!
Posted on September 11, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.