Git command examples

Before we see git command examples, let’s see what is Git.

What is Git

So basically GIT is the version control system, so let’s say you have multiple versions of the same file so GIT will help you in maintaining and saving those versions in LOG format. They were originally designed for coordinating work among programmers who were operating source codes during software development.

Git command examples

Git is a fast, scalable, and distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals.

Benefits of Git

  • It’s free and open source
  • Faster Performance
  • Widely Used

Git Clone

$ git clone <repository url>

Here’s an example of cloning a public repo of just geek

$ git clone https://github.com/justgeek-io/public-repo.git
Cloning into 'public-repo'...
remote: Enumerating objects: 35, done.
remote: Counting objects: 100% (35/35), done.
remote: Compressing objects: 100% (28/28), done.
remote: Total 35 (delta 7), reused 17 (delta 2), pack-reused 0
Unpacking objects: 100% (35/35), done.

Git Checkout

you can use checkout to switch to any branch. For example, if you want to switch to master then

$ git checkout master

if the branch doesn’t exist and you want to create a new one then use -b

$ git checkout -b new_branch_name

Git Add

When you are changing the files or adding new files then you need to add them.
For individual files

$ git add <file-path>

For all files

git add .

You could also use

$ git add -A

Git Push

Git push will push the locally committed changes to the remote branch.

$ git push

If you want to push to the original branch then use below

$ git push origin <branch-name>

5. Git Diff

You can use this command to see the unstaged changes on the current branch. Here’s an example of a branch with an edited feature file:

$ git diff

6. Compare Branches

You can compare the branches using the command

$ git diff <branch1> <branch2>

Git commit

This is the most used command after all your changes have been done. You will need to commit the changes. -m will add a comment

$ git commit -m "application.yaml file has been updated"

Git Revert

Sometimes you want to revert the changes which you have made for x number of reasons. There are a few steps, involved in revert. First, check the git logs.

$ git log

Example:

$ git log
commit de3ff79d3424c76c80a993d38d92e388b924ac0aq
Author: root <root@centos7.linuxvmimages.local>
Date:   Fri Aug 12 09:52:49 2022 -0400

    adding test.sh

From the above log copy the commit number and then run git commit <revert number> which will open you to the screen where you need to enter a comment, you can directly just save that file using ESC : wq! just like to write and save any Linux file using the vi command editor.

$ git revert de3ff79d3424c76c80a993d38d92e388b924ac0a
[master 78efb80] Revert "adding file"
 Committer: root <root@centos7.linuxvmimages.local>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 script.sh

and the last step is to push

Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 256 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/justgeek-io/public-repo.git
   de3ff79..78efb80  master -> master

Git Status

This command will show you all the necessary information about the current branch.

$ git status

Hope you have liked basic git command examples, also see All about SNMP If you have missed it.

Leave a Comment