Top 22 Git Commands With Examples | Zenesys Blogs

TOP 22 GIT COMMANDS WITH EXAMPLES

4 min May 24, 2021 Sandip Singh CLOUD Views : 1916
TOP 22 GIT COMMANDS WITH EXAMPLES

What Is Git?

Git is an open source distributed version-control system for tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows.

How Git Works?

  1. Git stores projects in repositories.
  2. Commits are made to the project and they tell Git that you are satisfied with the new or changed code you created.
  3. New code/changes are committed on branches. Most of the work is committed to other branches and then merged with the master branch.
  4. All this is stored in the same directory as the project but in a sub-folder called .git. 
  5. To share the code with your colleagues you push the changes to the repository. To get the new code from your colleagues, you pull changes from the repository.


FUNCTIONS OF VERSION CONTROL SYSTEM

● Allows multiple users / developers to work on a project simultaneously
● It doesn’t overwrite the work/changes of other’s
● Maintains a history of every change / versions.

Why Git Is Popular?

● FREE AND OPEN SOURCE

As it is an open source, you can download its source code and also perform changes according to your requirements.

● PERFORMANCE

Git provides better performance when it comes to version control system.Branching, committing, merging are all optimized to perform better than most of the version control systems.

● SECURITY

Git uses a common cryptographic hash function called secure hash function (SHA1).This algorithm manages the files , versions and folders securely so that the work is secure and not corrupted.

● DISTRIBUTED

Git is distributed in nature , which means the code on the git repository can be cloned or copied to the Developer’s systems so that he / she can work only on it.

● BRANCHING MODEL

Git has different branching models so that you can have multiple branches which are independent of each other and takes only a few seconds to create , merge and delete branches.

Different Types Of Version Control System

● Centralized version control system
● Distributed version control system

GIT Commands With Example

1) TELLING GIT WHO YOU ARE

Configure the author name and email address to be used with the commits.
git config --global user.email "youremail"
git config --global user.name "username"


2) CREATING NEW LOCAL REPOSITORY

Create a folder in your local machine and then run the below command within the directory.
git init
The above command will create a repository in your local system.

and If you run ls -la in the current directory , You can find a folder .git is created.

3) ADDING ONE OR MORE FILES TO GIT

To add a file to the Staging area
git add filename

To add more files to staging area
git add *

Staging area are the files that are going to be the part of the next commit.

4) COMMITING CHANGES

Commit is used for saving changes. After adding files to the staging area using git add, You can commit those changes but not to the remote repository.
git commit -m "commit message"

To commit any files that you have added with git add and also commit any files you have changed since then.
git commit -a

The above command only works for tracked files (files which are added to git using .git command).

5) CHECKING STATUS OF GIT

Lists all the files that you've changed that still need to be added or commited.
git status

The above command will list all the files/folders that are not yet committed.

6) TO LIST ALL THE LOCAL BRANCHES IN THE CURRENT REPOSITORY

The command lists all the local branches in the current repository.
git branch

7) TO CREATE A NEW BRANCH

This command will create a new branch.
git branch branchname

To check whether the branch is created or not , Run git branch


To delete a branch,
git branch -d branchname


8) SWITCHING BETWEEN BRANCHES

If you are working on different branches and If you want to switch between them.
git checkout branch


If you append -b to the above command , It will create a new branch and also switches to it.
git checkout -b branchname


9) COPYING REMOTE REPOSITORY TO LOCAL WORKING DIRECTORY

To create a clone or copy of the targeted remote repository to the local working directory.
git clone: https://Rahulmuthu80@bitbucket.org/Rahulmuthu80/latest.git


If the remote git repository which you are cloning to the local working directory is not public , Then it asks for a password.

10) FETCH & MERGE REMOTE REPOSITORY CHANGES TO LOCAL

The command will fetch for any changes in the remote repository and merge it with the local repository.
git pull https://Rahulmuthu80@bitbucket.org/Rahulmuthu80/latest.git

                      Also Read: Implementing CI/CD pipeline on Azure
 

11) TRACKING CHANGES

git diff command is used to track the difference between the changes made on the file.
git diff

The above command is used to find the differences between commits, branches, files, working trees, etc.
 
For example : The command will pick a file from the locally working directory and check for the changes.

It performs 2 checks , One to compare HEAD to staging area and one to compare staging area to work-tree.


12) CHECK DIFFERENCES BETWEEN STAGING AREA AND THE LATEST VERSION

git diff --staged

Lets say , You have a file which is already committed , Now you have made changes in that file and staged it.

To check the differences between the branches

The command will show the differences between the two branches.
git diff newbranch master

13) CHECK THE VERSION HISTORY OF CURRENT BRANCH

The command is used to list all the version history of the current branch. git log command lists the commits made in the branch in reverse order.
git log

To check the version history of a particular file,
git log --follow filename
 

14) TO CHECK LOG MESSAGES AND TEXTUAL DIFFERENCE OF A COMMIT

git show command is used to view the changes on the specific commit and the metadata of a commit
git show commitID


15) TO TAG A SPECIFIC COMMIT

This command is used to give a tag to a specific commit.
git tag v1.1 commitID

16) TO LIST THE TAGS

using the below command , You can list the tags.
git tag

If you want to list the tags with a specific tag pattern , append -l to the above command.
git tag -l v1*
 

17) MERGING BRANCHES

This command is used to merge the specified branch's history into the current branch.

For example : Let's say your current branch is newbranch and you want to merge it with master , If you run the below command all the changes in the master branch will be reflected in the newbranch.
git merge master


18) CONNECTING LOCAL REPOSITORY TO THE REMOTE REPOSITORY.

Using the below command , You can connect your local git repository to the remote git repository

git remote add origin
https://Rahulmuthu80@bitbucket.org/Rahulmuthu80/latest.git


19) PUSHING LOCAL BRANCH TO REMOTE REPOSITORY

if you have worked on a branch locally and you want to push that particular branch to the remote git repository, run the below command.

git push --set-upstream origin localbranchname


                    You May Also Like: Provisioning RDS Instances using Terrafor
 

20) STASHING GIT FILES

Using this command You can temporarily store all the modified tracked files.
git stash save

To restore the recently stashed files.
git stash pop

git stash list

To delete the stash
git stash drop stash@{0}

21) REMOVING A FILE FROM GIT

The below command will delete the file from the staging area (index) 
git rm filename --cached

if you want to delete the file from git and as well as the file system,use the below command.
git rm filename

22) GIT RESET

If you have staged a file and you want to unstage it ? , Use the below command. 
git reset filename

To reset all the changes after the specified commits , But the changes are locally preserved.
git reset commitID

Conclusion

We have learnt the most useful git commands with the examples. Keep checking out our website for more helpful articles!