DEV Community

AK DevCraft
AK DevCraft Subscriber

Posted on • Edited on

Git basic: essential commands - Part 1

This post contains a list of basic Git essential commands that are very handy.

1. Setup Git configs for new user

This will associate the user's details with a commit, this will set up Git configs at the global level:

  • Setup user name
git config --global user.name "User Name"
Enter fullscreen mode Exit fullscreen mode
  • Setup email address
git config --global user.email "someone@somehwere.com"
Enter fullscreen mode Exit fullscreen mode
  • List user's Git config
git config --list

--Output--

user.name=User Name
user.email=someone@somehwere.com
Enter fullscreen mode Exit fullscreen mode
  • Setup alias/shortcut for Git commands, e.g. setting status cmd as st
git config --global alias.st status
Enter fullscreen mode Exit fullscreen mode

2. Setup auto-completion

curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
Enter fullscreen mode Exit fullscreen mode

Configure script based upon type of shell you're using:

  • bash add below script in .bash_profile file under home directory e.g. /home/username/.bash_profile
  • zsh add below script in .zshenv file under home directory e.g. /home/username/.zshenv

Script:

if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
fi
Enter fullscreen mode Exit fullscreen mode

3. File difference in the working directory

3.1 Simple way to see file difference

git diff filename
Enter fullscreen mode Exit fullscreen mode

E.g.

ak@--mac git-useful-commands % git diff first-file.txt

--Output--

diff --git a/git-useful-commands/first-file.txt b/git-useful-commands/first-file.txt
index 86ac065..d2c8cc9 100644
--- a/git-useful-commands/first-file.txt
+++ b/git-useful-commands/first-file.txt
@@ -1 +1 @@
-my first file
+my first file modified
Enter fullscreen mode Exit fullscreen mode

3.2 Intuitive way to see file difference, it highlights what words were changed

git diff --color-words filename
Enter fullscreen mode Exit fullscreen mode

E.g.

ak@--mac git-useful-commands % git diff --color-words first-file.txt

--Output--

diff --git a/git-useful-commands/first-file.txt b/git-useful-commands/first-file.txt
index 86ac065..d2c8cc9 100644
--- a/git-useful-commands/first-file.txt
+++ b/git-useful-commands/first-file.txt
@@ -1 +1 @@
my first file **modified**
Enter fullscreen mode Exit fullscreen mode

4. File difference in staging index

4.1 Intuitive way to see file difference, it highlights what words were changed

git diff --staged --color-words filename
Enter fullscreen mode Exit fullscreen mode

E.g.

ak@--mac git-useful-commands % git diff --staged --color-words first-file.txt

--Output--

diff --git a/git-useful-commands/first-file.txt b/git-useful-commands/first-file.txt
index 86ac065..d2c8cc9 100644
--- a/git-useful-commands/first-file.txt
+++ b/git-useful-commands/first-file.txt
@@ -1 +1 @@
my first file **modified**
Enter fullscreen mode Exit fullscreen mode

Note: Remove --color-words option to get the difference in a regular way.

5. Remove/Delete file

It deletes the file and adds the change to the staging index

git rm filename
Enter fullscreen mode Exit fullscreen mode

E.g.

ak@--mac git-useful-commands % git rm third-file.txt 
rm 'git-useful-commands/third-file.txt'

--Output--

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)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    deleted:    third-file.txt

ak@--mac git-useful-commands % git commit -m "Delete third file"
[main 4dcfd37] Delete third file
 1 file changed, 1 deletion(-)
 delete mode 100644 git-useful-commands/third-file.txt

Enter fullscreen mode Exit fullscreen mode

6. Rename the file

It renames the file and adds the change to the staging index

git mv filename new-filename
Enter fullscreen mode Exit fullscreen mode

E.g.

ak@--mac git-useful-commands % git mv first-file.txt first-text-file.txt

--Output--

ak@--mac git-useful-commands % git st                                   
On branch main
Your branch is ahead of 'origin/main' by 4 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    renamed:    first-file.txt -> first-text-file.txt

ak@--mac git-useful-commands % git commit -m "Rename first file"
[main 3496e22] Rename first file
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename git-useful-commands/{first-file.txt => first-text-file.txt} (100%)
Enter fullscreen mode Exit fullscreen mode

7. Check last commit metadata

git log -n1
Enter fullscreen mode Exit fullscreen mode

E.g.

ak@--mac git-useful-commands % git log -n1

--Output--

commit 3496e22a89ce3990dcca942b9854acca5e0eff86 (HEAD -> main)
Author: Animesh <email@address.com>
Date:   Fri Sep 10 21:40:20 2021 -0500

    Rename first file
Enter fullscreen mode Exit fullscreen mode

Similarly use n2, n3, n4 ...nn to see last n commit's metadata

8. Check a particular commit details

git show hashvalue
Enter fullscreen mode Exit fullscreen mode

E.g.
Get the hash/SHA1 value of the commit

ak@--mac git-useful-commands % git log -n1
commit 3a17071be9814994d392eb392fd5c7d17f5cb4cf (HEAD -> main)
Author: Animesh <email@address.com>
Date:   Fri Sep 10 21:40:20 2021 -0500

    Rename first file
Enter fullscreen mode Exit fullscreen mode

3a17071be9814994d392eb392fd5c7d17f5cb4cf 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 show 3a17071

--Output--

commit 3a17071be9814994d392eb392fd5c7d17f5cb4cf (HEAD -> main)
Author: Animesh <email@address.com>
Date:   Fri Sep 10 21:40:20 2021 -0500

    Rename first file

diff --git a/git-useful-commands/first-file.txt b/git-useful-commands/first-file.txt
index d2c8cc9..a6d7017 100644
--- a/git-useful-commands/first-file.txt
+++ b/git-useful-commands/first-file.txt
@@ -1 +1,3 @@
 my first file modified
+
+Modify one more time
Enter fullscreen mode Exit fullscreen mode

I intended to keep the post short, hence I have divided it into two parts, part two is here Git basic: essential commands - Part 2

Happy Coding!

Top comments (0)