Important Git Commands
Today, Git is the most popular version control in the world (over 70% of developers use it). This makes Git one of the essential tools for development. This article lists the important (essential) Git commands to know: git config, git init, git clone, git add, git restore, git commit, git diff, git reset, git status, git rm, git log, git show, git tag, git branch, git checkout, git merge, git remote, git push, git pull, git stash. This list is just a reference and of course you need to learn Git in depth.
Commands
Below we will give a brief description of each command followed by examples.
git config
The git config command is a function that is used to set Git configuration values on a global or local project leve
Sets user name
$ git config --global user.name "[name]"
Sets user email
$ git config --global user.email "[email address]"
Enables helpful colorization of command line output.
$ git config --global color.ui auto
Shows your configuration
$ git config --list
Example
# set user name
[jmare@bigboss ~]$ git config --global user.name "samuel kokou"
# set user email
[jmare@bigboss ~]$ git config --global user.email "samuel@kokou.com"
# color setting
[jmare@bigboss ~]$ git config --global color.ui auto
# List config
[jmare@bigboss ~]$ git config --list
user.name=samuel kokou
user.email=samuel@kokou.com
color.status=auto
color.branch=auto
color.interactive=auto
color.diff=auto
color.ui=auto
merge.conflictstyle=diff3
core.editor=vim
git init
The git init command is used when starting a new repository. It turns an existing directory into a git repository. The command must be executed in the target directory.
$ git init
Example
[jmare@bigboss test]$ git init
Initialized empty Git repository in /home/jmare/Documents/test/.git/
git clone
The git clone command downloads an existing Git repository from the server to your local computer.
$ git clone
Example
[jmare@bigboss git]$ git clone https://github.com/jememare/dotfiles.git
Cloning into 'dotfiles'...
remote: Enumerating objects: 1147, done.
remote: Counting objects: 100% (368/368), done.
remote: Compressing objects: 100% (233/233), done.
remote: Total 1147 (delta 146), reused 265 (delta 91), pack-reused 779
Receiving objects: 100% (1147/1147), 40.44 MiB | 35.85 MiB/s, done.
Resolving deltas: 100% (551/551), done.
git add
The git add command adds new or changed files in your working directory to the Git staging area.
Stages all changes in
for the next commit.
$ git add
Stages all changes infor the next commit.
$ git add
Begins an interactive staging session that lets you choose portions of a file to add to the next commit.
$ git add -p
Example
# Check the working directory with git status
[jmare@bigboss dotfiles]$ git status
On branch master
Your branch is up to date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
newfile.txt
nothing added to commit but untracked files present (use "git add" to track)
# Add newfile.txt to staging area
[jmare@bigboss dotfiles]$ git add newfile.txt
# Check that newfile.txt is in the staging area
[jmare@bigboss dotfiles]$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: newfile.txt
git restore
The git restore command helps to unstage or even discard uncommitted local changes.
$ git restore
Example
# Check files in the staging area
[jmare@bigboss dotfiles]$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
modified: newfile.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
newd/
no changes added to commit (use "git add" and/or "git commit -a")
# Unstage README.md and newfile.txt
[jmare@bigboss dotfiles]$ git restore README.md newfile.txt
# Check if git status that the README.md and newfile.txt changes are removed
[jmare@bigboss dotfiles]$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Untracked files:
(use "git add <file>..." to include in what will be committed)
newd/
nothing added to commit but untracked files present (use "git add" to track)
git commit
The git commit command is used to save your changes to the local repository.
Saves currently staged files.
$ git commit -m "message"
Saves all currently changed files.
$ git commit -m -a "message"
Rewrites the very last commit with any currently changes and/or a new commit message.
$ git commit --amend -m "new message"
Example
# Check stage files
[jmare@bigboss dotfiles]$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: newfile.txt
# Save the stage files
[jmare@bigboss dotfiles]$ git commit -m "add new file for demo."
[master a360547] add new file for demo.
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 newfile.txt
git diff
git diff command is used in git to track the difference between the changes made on files
Shows the difference on all tracked files
$ git diff
Shows the difference on a given file
$ git diff
Example
[jmare@bigboss dotfiles]$ git diff README.md
diff --git a/README.md b/README.md
index 764a4f5..8f2e030 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,5 @@
# Developement Environnement Configuration
+Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
![screenshot](images/Screenshot01.png)
git reset
The git reset current HEAD to the specified state.
Undoes the last commit and put the changes in the working directoy.
$ git reset --soft HEAD~1
Undoes the last commit and lose the changes.
$ git reset --hard HEAD~1
Undoes all commits that came after the one you specified.
$ git reset --hard <object>
Example
[jmare@bigboss dotfiles]$ git reset --hard HEAD~1
HEAD is now at 99c73d4 start using lua for nvim - experimental
git status
The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git. Status output does not show you any information regarding the committed project history.
$ git status
Example
[jmare@bigboss portfolio]$ git status
On branch deuxieme
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: components/blog/blogPinned.module.css
modified: components/picture/picture.js
modified: components/slide/slideshow.module.css
deleted: content/blogs/important_git_command.md
modified: content/blogs/javascript_closure.md
deleted: content/blogs/javascript_promise.md
deleted: content/blogs/measure_unity_in_css.md
modified: pages/blogs/post/[slug].js
modified: styles/blogs.module.css
Untracked files:
(use "git add <file>..." to include in what will be committed)
content/blogs/css_units_best_practices.md
content/blogs/important_git_commands.md
content/blogs/recaptcha_v3_with_nextjs.md
lib/importBlogsPosts.js
public/admin/
no changes added to commit (use "git add" and/or "git commit -a")
git rm
The git rm command helps you to remove files from a Git repository.
Removes file from Git repository and from the filesystem.
$ git rm <file>
Removes file from Git repository, but not from the filesystem.
$ git rm <file> --cached
Example
[jmare@bigboss dotfiles]$ git rm newfile.txt
rm 'newfile.txt'
git log
The git log command helps to view the commit history.
$ git log
Example
[jmare@bigboss dotfiles]$ git log
commit 392925bbdd027354c3622237f9107ca2d2aab529 (HEAD -> master)
Author: Jeme Mare <jememare@gmail.com>
Date: Thu Aug 5 11:32:59 2021 -0400
add new file
commit 99c73d41bf3ff48e662d5ca7340d28e84a3d71df (origin/master, origin/HEAD)
Author: Jeme Mare <jememare@gmail.com>
Date: Sun Aug 1 21:30:02 2021 -0400
start using lua for nvim - experimental
commit 7facfdaccf0fae2a337007ee2773cc8f1e0cf740
Author: Jeme Mare <jememare@gmail.com>
Date: Sun Aug 1 21:21:23 2021 -0400
reorganize file system to use stow
commit 28180fca569d83ba7d2506a017bcf0a56561d7b0
Author: Jeme Mare <jememare@gmail.com>
Date: Sun Aug 1 20:42:21 2021 -0400
git show
The git show command shows the metadata and content changes of the specified commit.
$ git show <SHA>
Example
[jmare@bigboss dotfiles]$ git show 2448e291b99b15de02b26f2d7e8ad43f840d386d
commit 2448e291b99b15de02b26f2d7e8ad43f840d386d
Author: Jeme Mare <jememare@gmail.com>
Date: Fri Jul 30 17:30:22 2021 -0400
change terminal setting
diff --git a/tmux.conf b/tmux.conf
index d42fad7..eecf17f 100644
--- a/tmux.conf
+++ b/tmux.conf
@@ -16,7 +16,8 @@ bind -n M-l select-pane -R
# Color
set -g default-terminal "screen-256color"
-set -ga terminal-overrides ",*256col*:Tc"
+# set -ga terminal-overrides ",*256col*:Tc"
+set-option -sa terminal-overrides ',alacritty:RGB'
# copy mode
set-window-option -g mode-keys vi
@@ -71,3 +72,5 @@ set-option -g status-right "#[fg=colour239, bg=colour237, nobold, nounderscore,
set-window-option -g window-status-current-format "#[fg=colour237, bg=colour214, nobold, noitalics, nounderscore]#[fg=colour239, bg=colour214] #I #[fg=colour239, bg=colour214, bold] #W #[fg=colour214, bg=colour237, nobold, noitalics, nounderscore]"
set-window-option -g window-status-format "#[fg=colour237,bg=colour239,noitalics]#[fg=colour223,bg=colour239] #I #[fg=colour223, bg=colour239] #W #[fg=colour239, bg=colour237, noitalics]"
+
git tag
The git tag command lets you create, list and delete tags.
Creates a new lightweight tag
$ git tag <tagname>
Creates a new annotated tag
$ git tag -a <tagname>
Lists tags
$ git tag
Deletes tag
$ git tag -d <tagname>
Example
# create a lightweight tag v1.4-lw
[jmare@bigboss dotfiles]$ git tag v1.4-lw
# list all tags
[jmare@bigboss dotfiles]$ git tag
v1.4-lw
# delete the tag v1.4-lw
[jmare@bigboss dotfiles]$ git tag -d v1.4-lw
Deleted tag 'v1.4-lw' (was 392925b)
git branch
The git branch command lets you create, list, rename, and delete branches.
Lists all of the branches in your repository. This is synonymous with git branch --list.
$ git branch
Creates a new branch called <branch>. This does not check out the new branch.
$ git branch <branch-name>
Deletes the specified branch. This is a “safe” operation in that Git prevents you from deleting the branch if it has unmerged changes.
$ git branch -d <branch-name>
Forces delete the specified branch, even if it has unmerged changes.
$ git branch -D <branch-name>
Renames the current branch.
$ git branch -m <branch>
Example
# Create a new branch name new
[jmare@bigboss dotfiles]$ git branch new
# List all branches
[jmare@bigboss dotfiles]$ git branch
* master
new
# Create another branch name old
[jmare@bigboss dotfiles]$ git branch old
# List again all branches
[jmare@bigboss dotfiles]$ git branch
* master
new
old
# Delete the branch old
[jmare@bigboss dotfiles]$ git branch -d old
Deleted branch old (was 392925b).
# List all branches to check if old branch is deleted
[jmare@bigboss dotfiles]$ git branch
* master
new
git checkout
The git checkout command can switch the currently active branch.
Switch the currently active branch
$ git checkout <branch-name>
Creates a new local branch and directly switches to it.
$ git checkout -b <new-branch-name>
Example
[jmare@bigboss dotfiles]$ git checkout -b clone
Switched to a new branch 'clone'
git merge
The git merge command merges the specified branch’s history into the current branch.
$ git merge [branch name]
Example
[jmare@bigboss dotfiles]$ git merge new
Merge made by the 'recursive' strategy.
newd/file.txt | 1 +
newfile.txt | 4 ++++
2 files changed, 5 insertions(+)
create mode 100644 newd/file.txt
create mode 100644 newfile.txt
git remote
The git remote command helps you to manage connections to remote repositories.
Lists the remote connections you have to other repositories.
$ git remote -v
Creates a new connection to a remote repository.
$ git remote add <name> <url>
Removes the connection to the remote repository.
$ git remote rm <name>
Rename a remote connection
$ git remote rename <old-name> <new-name>
Example
[jmare@bigboss dotfiles]$ git remote -v
origin https://github.com/jememare/dotfiles.git (fetch)
origin https://github.com/jememare/dotfiles.git (push)
git push
The git push command uploads all local branch commits to the corresponding remote branch.
$ git push
Example
[jmare@bigboss huddle-landing-page]$ git push origin master
Username for 'https://github.com': jememare
Password for 'https://jememare@github.com':
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 4 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 6.25 KiB | 3.13 MiB/s, done.
Total 4 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
remote:
remote:
To https://github.com/jememare/huddle-landing-page.git
b088d36..f6f1666 master -> master
git pull
The git pull command is used to download and integrate remote changes.
$ git pull
Example
[jmare@bigboss dotfiles]$ git pull
warning: Pulling without specifying how to reconcile divergent branches is
discouraged. You can squelch this message by running one of the following
commands sometime before your next pull:
git config pull.rebase false # merge (the default strategy)
git config pull.rebase true # rebase
git config pull.ff only # fast-forward only
You can replace "git config" with "git config --global" to set a default
preference for all repositories. You can also pass --rebase, --no-rebase,
or --ff-only on the command line to override the configured default per
invocation.
remote: Enumerating objects: 36, done.
remote: Counting objects: 100% (36/36), done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 34 (delta 6), reused 34 (delta 6), pack-reused 0
Unpacking objects: 100% (34/34), 5.07 KiB | 1.69 MiB/s, done.
From https://github.com/jememare/dotfiles
99c73d4..13bdf57 master -> origin/master
Updating 99c73d4..13bdf57
Fast-forward
{systemd => settings}/alert_battery/.config/systemd/user/alert-battery.service | 0
{systemd => settings}/alert_battery/.config/systemd/user/alert-battery.timer | 0
settings/stow/.stowrc | 2 ++
{settings_alt/nvimlua => settings/w_o_nvimlua}/.config/nvim/colors/gruvbuddy.vim | 0
{settings_alt/nvimlua => settings/w_o_nvimlua}/.config/nvim/init.lua | 2 ++
{settings_alt/nvimlua => settings/w_o_nvimlua}/.config/nvim/lua/gruvbuddy.lua | 0
{settings_alt/nvimlua => settings/w_o_nvimlua}/.config/nvim/plugin/lsp.lua | 0
{settings_alt/nvimlua => settings/w_o_nvimlua}/.config/nvim/plugin/lualine.lua | 0
{systemd => settings}/wallpaper/.config/systemd/user/wallpaper.service | 0
{systemd => settings}/wallpaper/.config/systemd/user/wallpaper.timer | 0
10 files changed, 4 insertions(+)
rename {systemd => settings}/alert_battery/.config/systemd/user/alert-battery.service (100%)
rename {systemd => settings}/alert_battery/.config/systemd/user/alert-battery.timer (100%)
create mode 100644 settings/stow/.stowrc
rename {settings_alt/nvimlua => settings/w_o_nvimlua}/.config/nvim/colors/gruvbuddy.vim (100%)
rename {settings_alt/nvimlua => settings/w_o_nvimlua}/.config/nvim/init.lua (98%)
rename {settings_alt/nvimlua => settings/w_o_nvimlua}/.config/nvim/lua/gruvbuddy.lua (100%)
rename {settings_alt/nvimlua => settings/w_o_nvimlua}/.config/nvim/plugin/lsp.lua (100%)
rename {settings_alt/nvimlua => settings/w_o_nvimlua}/.config/nvim/plugin/lualine.lua (100%)
rename {systemd => settings}/wallpaper/.config/systemd/user/wallpaper.service (100%)
rename {systemd => settings}/wallpaper/.config/systemd/user/wallpaper.timer (100%)
git stash
The git stash command takes your uncommitted changes, saves them away for later use, and then reverts them from your working copy.
Temporarily stores all the modified tracked files.
$ git stash
Restores the most recently stashed files.
$ git stash pop
Lists all stashed changesets.
$ git stash list
discards the most recently stashed changeset.
$ git stash drop
Example
# Check repo status
[jmare@bigboss dotfiles]$ git status
On branch clone
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: newfile.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
newd/
no changes added to commit (use "git add" and/or "git commit -a")
# Store all the modified tracked files
[jmare@bigboss dotfiles]$ git stash
Saved working directory and index state WIP on clone: 392925b add new file
# Check status again and notice that modified tracked files are gone
[jmare@bigboss dotfiles]$ git status
On branch clone
Untracked files:
(use "git add <file>..." to include in what will be committed)
newd/
nothing added to commit but untracked files present (use "git add" to track)
# Restore the save tracked files
[jmare@bigboss dotfiles]$ git stash pop
On branch clone
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: newfile.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
newd/
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (0b35a8f42debdd3fde9b88c71cd775e08b0ad0be)
If you want to learn Git in depth, you can check out this free book.