Git Commands you must know
Git Commands you must know:
Developed by Linus Torvalds in 2005, Git is a Version Control Appication which is very much popular among the developers. Though there are many version control applications. Git is one of the most popular and most used one. The real importance of git is that it is extremely light weighted while still being highly robust and flexible. Furthermore, it is completely distributed and doesn’t need any central server system. Also, it doesn’t even need network connectivity, you can use it in self-contained manner in your own device. So, it’s not a surprise about it being the predominant VCS in the workplace.
So, having some knowledge and skills for using git is a must if you are a developer.
Below we have some basic commands that are required for your day to day operation.
- git config:
one of the most used command, it is used for setting user configuration attributes like email, username, file format, etc
For example for setting email we have the command:
git config --global user.email abc@gmail.com
- git init:
This command is used for creating new GIT repository.
use: git init
- git status:
This command is used for checking the list of files changed along with the files that needs to be added or commited.
- git log:
Using git log we can study and get details of the repository history
For eg in order to see only the commits of certain author
git log –author=Anil
- git add:
This command is used for adding new files to the index. For example lets add a file named home.html form local directory to index.
git add home.html
- git commit:
This command is used for committing the changes to the head. Committed changes does not make it to the remote repository yet .
git commit –m “Message about the changes”
- git push:
Another most used commands that transfers the changes to the master branch of the remote repository associated with the working directory. You can change the master to whatever branch you are willing to make the change
git push origin master
- git pull:
This commands is also the most used one which lets us merge all the changes to our local working directory. Updating the local repository to the newest commit
git pull
- git merge:
This command is used for merging a branch into our active branch (eg master). Use
git merge <branch-name>
- git reset:
In order to reset the working directory to the previously committed state
git reset – hard HEAD
- Creating shortcuts in git:
We may get bored typing long git syntax time after time. To get over it we can create shortcuts. First we need git bash editor which leads us to the home directory. There you can find and edit your .bash_profile file where we can add the shortcuts . It can be done by adding text as
alias gc=’git commit’
alias gr=’git revert’
- Storing Username and Password in git:
Some us have come across a situation where we need to repeatedly enter the user credentials while executing the push, pull and commit commands. Mostly happens when we use Tortoise Git
We can solve it by following steps
- Navigate to the root directory thrugh CMD
- Enter git config credential.helper store then,
- Pull the repo with git pull
There you are asked for username and password, once provided the credentials are stored and not asked repeatedly.
- Branching in git:
Branches are used for developing features isolated from each other.
When creating a repository Master Branch is the default branch. We can create other ranches for development and collaborate them back to the master repo.
Lets create a new branch named “Dashboard”
Git checkout –b feature_x
Switch back to master
git checkout master
Deleting the branch
git branch –d feature_x
The branch is not available to other until we push the branch to the remote repository using
Git push origin <branch>
This are some of the most commonly used git commands but we also have a sheet containing all of these commands that may be useful for you.
Follow the link : Git Command Sheet
Also Read: Programming Languages to learn in 2019