Git basic: essential commands - Part 2

akdevcraft

AK DevCraft

Posted on September 11, 2021

Git basic: essential commands - Part 2

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

E.g.

ak@--mac git-useful-commands % git checkout -- second-file.txt
ak@--mac git-useful-commands % git restore second-file.txt 
Enter fullscreen mode Exit fullscreen mode

11. Undo file in the staging index

git restore --staged filename
Enter fullscreen mode Exit fullscreen mode

E.g.

ak@--mac git-useful-commands % git restore --staged second-file.txt
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

E.g.

ak@--mac git-useful-commands % git commit --amend -m "Update second file"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

E.g.
Get hash/SHA1 value of the commit you want to revert

ak@--mac git-useful-commands % git revert 4743e81de
Enter fullscreen mode Exit fullscreen mode

15. Remove untracked file from the working directory

git clean -f
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

Later stage and commit

ak@--mac git-useful-commands % git add temp/.gitkeep
ak@--mac git-useful-commands % git commit -m "Add temp dir"
Enter fullscreen mode Exit fullscreen mode

17. Directly commit the tracked file

git commit -am filename
Enter fullscreen mode Exit fullscreen mode

E.g.

git commit -am first-file.txt
Enter fullscreen mode Exit fullscreen mode

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!

💖 💪 🙅 🚩
akdevcraft
AK DevCraft

Posted on September 11, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related