In Morden days most of the software industry using Git as source control
Git is an important part of daily programming especially if you're
working with a team and is widely used in the software industry.
In this article, We are going to learn some basic and advance Git
command which we generally use daily basis.
Git is a version control system for managing the source code which keeps
the track of it with many options. Basically, it is a software to track the
changes of files mostly used for coders to work collaboratively and source code
management during the software development.
As per Git-SCM (https://git-scm.com/)
Git is a free and open-source distributed version control system
designed to handle everything from small to very large projects with speed and
efficiency.
Basic Git Commands
In this section, we will learn some of the basic Git commands.
1. git Init
This is probably the first command you use to start
a new project in Git. This command will create a blank new repository, and then
you can store your source code inside this repo.
Git Command
Or you can use the repository name with your git
init command.
git init < repository
name>
|
2. git config
Git config command is used when you are using Git for the first
time, or you have a new Git installation.
This command will set up your identity - Name and Email address.
And this information will be used with every commit.
Git Command
git
config global user.name "Your name"
|
git config global user.email "Your
email"
|
3. git version
As its name implies, it's just to check which version of Git you
are using. At the moment, writing this guide, the latest version of Git for
Windows is 2.33.1. It was released on 12th Oct 2021.
Git Command
You can get Git for windows from below link
https://git-scm.com/
4. git clone
The git clone command will use an existing repository to copy.
There is one main difference between the git init and git clone.
You will use the Git clone when you need to make a copy on an
existing repository. The git clone command internally uses the git init command
first and then checks out all its contents.
Git Command
git clone
<your project URL>
|
5. git add
The Git add command will add all the new code files or modified
files into your repository. This command offers different options to add files
and folders.
Git Command
git add your_file_name
(it
will add a single file to your staging area)
git
add *
( this option will add all the modified and new files to
the staging area)
6. Git Remove
To remove the file or folder from your working directory and
stages deletion.
Git Command
git rm
[file_name]
|
git rm -r
[file_name]
|
7. git commit
This Git command is essential. Your project quality may drop if
you will not use this command appropriately.
In simple words, the Git commit will add your changes to your
local repository.
Git Command
git
commit m “your commit comment”
|
8. git status
This Git command is convenient to see how many files are there
which need your attention. You can run this command at any time.
You can use it in between Git add, and Git commits to see the
status.
Git Command
9. git branch
Most of the time, you have multiple branches in your Git
repository. In simple terms, the branch is an independent line of code
development.
With the Git branch command, you can manage your branches
effectively. There are many different options and switches of the Git
branch.
Git Command
1. To list all
branches:
2. To create a
new branch:
3. To delete a
branch:
git branch d <branch_name>
|
4.
To delete the remote branch.
git push origin delete [branch_name]
|
10. git checkout
This Git command is used to switch between branches. This is one
of the powerful git commands and can use used as a swiss knife,
In simple words, here is the syntax to switch to another branch.
Git Command
git checkout
<branch_name>
|
Also, you can create and checkout to a branch in a single like,
here is the usage for that
git checkout b <your_new_branch_name>
|
11. git push
Once you are connected with the remote repository (with the help
of the git remote command), it's time to push your changes to that repository.
Git Command
git
push u <short_name> <your_branch_name>
|
git push u origin feature_branch
|
You should have origin and upstream set up before you use Git push. And here is
the command to set up upstream.
Git Command
git push --set-upstream <short_name>
<branch_name>
|
git push --set-upstream origin
feature_branch
|
12. git fetch
When you need to download other team members' changes, you have
to use git fetch.
This command will download all information for commits, refs,
etc., so you can review it before applying those changes in your local
repository.
Git Command
13. git pull
The Git pull command downloads the content (and not the
metadata) and immediately updates your local repository with the latest
content.
Git Command
Intermediate Level Git Commands
After the basic here is the some Intermediate level of commands
Also, there are commands like Git log that will help to see the history of
previous commits.
14. git remote
Git remote command acts like a border, and If you need to
connect with the outside world, you have to use the Git remote command. This
command will connect your local repository to the remote.
Git Command
git remote add <shortname> <url>
|
15. git stash
This Git command temporarily stores your modified files. You can
work in stashed with the following Git command.
Git Command
And you can view all of your stashes with the following
command
And if you need a apply a stash to a branch, simply use
apply
16. git log
With the help of the Git log, you can see all the previous commits
with the most recent commit appear first.
Git Command
By default, it will show you all the commits of the currently
checked out branch, but you can force it to see all the commits of all the
branches with all options.
17. git shortlog
The shortlog command shows you a summary from the Git log
command. This command is helpful if you are just interested in the short
summary.
This command is helpful to see who worked on what as it group
author with their commits.
Git Command
18. git show
Compared to the Git log, this command git show will show you
details about a specific commit.
Git Command
git show <your_commit_comment>
|
19. git rm
Sometimes you need to delete files from your codebase, and in
that case, you can use the Git rm command.
It can delete tracked files from the index and the working
directory.
Git Command
20. git merge
Git merge helps you to integrate changes from two branches into
a single branch.
Git Command
This command will merge the <branch_name> into your
current selected branch.
Advanced Level Git Commands
Here are the some advance level of git command ,which we use in
our project as per needed.
21. git rebase
Git rebase similar to the git merge command. It integrates two
branches into a single branch with one exception. A git rebase command rewrites
the commit history.
You should use the Git rebase command when you have multiple
private branches to consolidate into a single branch. And it will make the
commit history linear.
Git Command
22. git bisect
The Git bisect command helps you to find bad commits.
Git Command
Ø To start the
git bisect
Ø let git
bisect know about a good commit
Ø And let git
bisect know about a bad commit
With Git bisect you can narrow down the broken code within a few
minutes.
23. git cherry-pick
Git cherry-pick is a helpful command. It's a robust command and allows
you to pick any commit from any branch and apply it to any other branch.
Git Command
git cherry-pick <commit-hash>
|
Git cherry-pick doesn’t modify the history of a repository;
instead, it adds to the history.
24. git archive
Git archive command will combine multiple files into a
single file. It's like a zip utility, so it means you can extract the
archive files to get individual files.
Git Command
git archive --format zip HEAD >
archive-HEAD.zip
|
It will create a zip archive of the current revision.
25. git pull --rebase
Most of the time, you need to do rebase (and no merge) when you
use Git pull.
In that case, you can use the option
Git Command
It will help you to keep the history clean. Also, you can avoid multiple
merges.
26. git blame
If you need to examine the content of any file line by line,
you need to use git blame. It helps you to determine who made the changes
to a file.
Git Command
git blame <your_file_name>
|
27. git tag
In Git, tags are helpful, and you can use them to manage the
release. You can think of a Git tag like a branch that will not change. It is
significantly more important if you are making a public release.
Git Command
28. git verify-commit
The git verify-commit command will check the gpg signature. GPG
or “GNU Privacy Guard” is the tool used in sign files and contains their
signatures.
Git Command
git verify-commit <commit>
|
29. git verify-tag
In the same way, you can confirm a tag.
Git Command
30. git diff
Most of the time, you need to compare two git files or branches
before you commit or push. Here is a handy command to do that.
Git Command
Ø to compare
the working directory with the local repo:
Ø to compare
two branches:
git diff <source branch> <target
branch>
|
31. git citool
Git citool is a graphics alternative of the Git commit.
Git Command
32. git mv
To rename a git file. It will accept two arguments, source and
target file name.
Git Command
git mv <old-file-name>
<new-file-name>
|
33. git clean
You can deal with untracked files by using the Git clean
command. You can remove all the untracked files from your working directory by
using this command.
Git Command
34. git help
There are many commands in Git, and if you need more help with
any command, you can use git help at any time from the terminal.
Git Command
35. git whatchanged
This command does the same thing as git log but in a raw form.
And it’s in the git because of historical reasons.
Git Command
Conclusion
In this article, I have shared the most used git commands, especially
for developers. There are several other commands available for git, however,
above mentioned are the topmost and popular commands.