Important Git Commands: Git Commands You Must Know
Tags: github, codenewbie, git
Published: 2020-08-30 10:08:57

Git is a very popular and widely used distributed version control system that helps developers manage their code efficiently by keeping a track record of the changes committed to the code-base by time. A VCS or version control system as it sounds is simply a tracker of content(usually code) that tracks changes to code-base and helps developers to simultaneously work on the same project by managing repositories. Git is a distributed version control system(DVCS) which simply means that Git stores code-base on a repository in a Server and simultaneously distribute it to the local repository(usually in Developer's Computer) of each developer.
So, No matter if you are new to development or have ample amount of experience in the development field, these are the essential git commands you must know. Lets have a look:

#Git Commands

###1) git config [ options ]
This command helps you set the username and email address to be used with your commits respectively.
**usage:** `git config --global user.name "John Doe"`
**usage:** `git config --global user.email "john.doe@gmail.com"`

###2) git init [ repository name ]
This command initializes a repository as git repository. To perform git operations you need to initialize the target repository as a git repository.
**usage:** `git init /home/john/Documents/git`
The folder git will be initialized as a git repository here.
Alternatively you can go into that repository and then initialize it by simply giving the command `git init`

###3) git clone [ URL ]
This command is used to download an existing repository from  a server by an existing URL.
**usage:** `git clone https:\\www.github.com\johndoe\example.git`

###4) git status
This command is used to see the status of the working tree. It shows you the list of unstaged files and list of files that needs commit. If there is nothing do then it will simply show that the branch is clean.
**usage:** `git status`
*Note:* You need to be in a git repository to know its status.

###5) git add [ File Name ]
This command puts the desired file to the staging area for operations to performed on it.
**usage:** `git add index.html`
*Note:* You can add multiple files to the staging area by separating files with a space - `git add index.html main.css main.js`
Alternatively you can use `git add . ` to add all the files in the repository to the staging area.

###6) git commit [ options ]
This command records changes to the git repository by saving a log message together with a commit id of the changes made.
**usage:** `git commit -m "Write your message here"`
Alternatively you can use `git commit -a` to commit changes to the files added using `git add` without specifying a message
*Note:* You should always commit with a message.
 

###7) git remote [ options ] [ variable name ] [ URL ]
This command is used to connect your local repository to the  the remote repository over a server.
**usage:** `git remote add origin https://www.github.com/johndoe/example.git`

###8) git push [ options ] [ variable name ] [ branch ]
This command is used to push the contents of your local repository to the added remote repository. This sends the committed changes of your master branch to the added remote repository.
**usage:** `git push -u origin master`
**Note:** `-u` depicts upstream here. 
Alternatively you can use `-f` instead of `-u` to forcefully push the contents of your repository.

###9) git pull [ URL ]
This command is used to fetch and integrate the contents of the remote repository to your local repository.
**usage:** `git pull https://www.github.com/johndoe/example.git`

###10) git checkout [ branch name ]
This command is used to move from one branch to another
**usage:** `git checkout 'branch_1'`

###11) git checkout [ options ] [ branch name ]
**usage:** `git checkout -b branch_2` is used to create specified branch and is simultaneously switches to it. 

###12) git branch [ options ] [ branch name ]
This command is used to perform operations over the specified branch
**usage:** `git branch -d branch_1` to delete the specified branch.
**Note:** You can use `-D` to forcefully delete a branch
usage: `git branch -m old_name new_name` to rename the branch
**usage:** `git branch -c branch_1 /home/johndoe/Documents` to copy the branch and corresponding reflog
**usage:** `git branch -C branch_1 /home/johndoe/Documents` to forcefully copy the branch
usage: `git branch -M branch_1 /home/johndoe/Documents` to forcefully move the branch
**usage:** `git branch --list` to list all the branches

###13) git merge [ branch name ]
This command is used to merge the history of the specified branch into the current branch.
**usage:** `git merge branch_3`

###14) git log
This command is used to show the log of commits made so far to the current branch.
**usage:** `git log`
**usage:** `git log --follow John_Doe` to see the log of commits made together with renaming of files of the specified file

###15) git show [ commit id ]
This command is used to list the metadata for the specified commit
**usage:** `git show 521747298a3790fde1710f3aa2d03b55020575aa`
**Note:** You can use git log to see all the commit ids

*For a full list of Git commands or Git cheat-sheet you can visit this link:* 
[Git-Cheat-Sheet](https://github.com/chaitanya4vedi/Git-Cheat-Sheet/blob/master/README.md)

Thanks For Reading ;)