In this post I am providing you 15 Git Commands every developer must know.
At the start, I was confused about git commands, once my senior asked me to create a new branch and always push the update to that branch from git. I can’t explain how confused I was at that time, I started searching the Git Commands.
I’m sure I can list at least a dozen occasions where I was confused with Git commands and what they did. So, that I am sharing a few important and frequently used Git commands.
Here are 15 basic commands that can speed up your development workflow. It allows you to go from a brand new repository to creating a new branch and merging it to the master branch.
Git Commands
1. Git Init
This command is usually the first command that you run in any new project which is not a Git repository or repo
It can be use to initialize an empty repo and it is also used to convert an existing folder into a Git repository.
Git Init Command
First go to the directory where you want to initialize with cd command.
Then, run this command
$ git init
git init
is one way to start a new project with Git. The current directory transforms into a Git repository and it creates a hidden directory called .git which allows you to start recording multiple versions of your projects.
2. Git Clone
The Git Clone is used to download the source code from the remote repository
The git clone command is used to create a copy of a specific repository or branch within a repository.
$ git clone https://url-of-the-repository
After running the code, it will be automatically downloaded to your local machine.
git clone [url]: Clone (download) a repository that already exists on GitHub, including all of the files, branches, and commits.
3. Git Branch
The branch is one of the most important features of Git. This allows teams to work on the same code base in parallel. It enables teams to create Git workflows and make their workflows more efficient
Using Git Branch to create a new branch
$ git branch
This creates a new branch only in your local system and if you want to show this branch to all the members in the repo, then you have to push the branch.
To push a newly created branch, run git push -u
This command is used to show all the branches in the repository.
Command to view all branches
$ git branch
$ git branch --list
If you want to delete any particular branch the command is
$ git branch -d
4. Git Checkout
The git checkout command is used to switch between branches in a repository.
The git checkout command operates upon three different entities which are files, commits, and branches
Git Checkout Command
$ git checkout
It will show the list of branches in the repo.
Create and Switch Branch
The git checkout commands let you create and switch to a new branch. You can not only create a new branch but also switch it simultaneously by a single command.
$ git checkout -b
5. Git Add
The git adds command adds new or changed files in your working directory to the Git staging area.
Git add file
$ git add
Git add command is a straightforward command. It adds files to the staging area. We can add single or multiple files at once in the staging area.
Git Add All
$ git add .
The above command will add all the files available in the repository.
6. Git Show
This command shows the metadata and content changes of the specified commit.
$ git show [commit]
7. Git Tags
Tags make a point as a specific point in Git history.
$ git tag
8. Git Commit
It’s commonly used to save your changes. Maybe after completing a specific work item assigned to you in your agile tool.
Each time you commit your code changes, it will also include a message to briefly describe the changes you made. This helps other team members quickly understand what was added, changed, or removed.
Using Git Commit Command
$ git commit -a
This will commit all changes to the directory you are working in. Once this command is run, you will be prompted to enter a confirmation message.
Alternatively, you can enter the confirmation message in the command itself and skip the additional step where you will be prompted to enter the confirmation message.
To do this, run the following git command:
$ git commit -am “”
9. Git Push
To make all committed changes available to your teammates, you’ll need to push them to the remote source.
Using Git Push Command
$ git push
Copy
It is important to remember that the git push command will load only the changes that you have committed.
10 .Git Pull
The git pull command allows you to get all the changes your teammates pushed and automatically merge them into your local repository.
Using Git Pull Command
$ git pull
In many cases, you will run into a conflict because you changed a line in a file that was added by another teammate. In such cases, you must resolve the conflicts manually.
11. Git Diff
Git Diff is my reference command when I want to quickly see the difference between my current branch and another branch (usually the branch I am merging into).
Using Git Diff Command
$ git diff
To compare two branches
$ git diff branch1..branch2
To compare a file from two branches
$ git diff branch1 branch2 ./path/to/file.txt
12. Git Stash
Git Stash temporarily archives your work, so you can switch to another branch, work on something else, and then come back to this later.
$ git stash save “”
When you want to view all the stashed code
$ git stash list
This will automatically restore and apply the topmost stash in the stack.
$ git stash apply
13. Git Status
When you’re feeling a bit lost with what happened in your repository (yes, it can happen), the Git Status command can tell you all the information you need to know.
Using the Git status command
$ git status
14. Git Log
While git status gave you almost all the information you would have needed, it would not give you the information about the commit history of the repository.
Using Git Log Command
$ git log
15. Git Merge
Once you are done with developing within your function branch and have tested your code, you can merge your branch with the main branch. This could be a development branch or a master branch depending on the git workflow you follow.
When running a git merge command, you must first be on the specific branch that you want to merge with your feature branch.
Using Git Merge Command
We must first switch to the develop branch using the checkout command.
$ git checkout develop
Before merging, you must make sure that you update your local development branch.
$ git pull
We do this by using the git merge command followed by the branch name that we want to merge into our current branch.
$ git merge feature1
These are some important git command that every developer must know about it
Also Read: