While working on a big project with a developer team, we use a version control system (VCS) that keeps track of the project code and maintains all the versions of it. Therefore, if we want to roll back to the previous version, we can do that easily. Git is the most popular, fast, and scalable version control system, and it works with commands. Here in this article, we have provided some of the most important Git commands that you should know if you want to use Git for version control.
If we do not use a version control system, we will not be able to manage project versions properly.
For example, if we create a new project that contains a lot of files and after a week we want to add some new functionality, we would add some new code or edit some code in the previous file and create a new version of that project.
This all process of changing or modifying the previous version of projects can be tracked by a version control system like Git so that other developers that are working on the same project can see what changes have been made on the project and what was the previous code has.
Please note that here we have not covered all the commands of Git because there are hundreds of Git commands, and it is impossible to go through and learn each one of them. Thus, we focus only on some of the most important Git commands here that are enough to serve our purpose.
What is Git?
Git is an open-source distributed version control system (DVCS). It is an application that keeps track of your project version and helps you to manage the project. Being a distributed VCS means that all the members of a team that are working on the same project would have the complete version of the project.
- Keeps track of project files.
- Tracks all the changes made in the project.
- Records all the changes.
- It can restore an old version of the project.
- Helps to compare the new code and old code.
- Distributes the same project to different developers.
- Merges code from a different computer.
Git Commands List
Here we are sharing a curated list of Git Commands that are executed by the developers on a day-to-day basis:
1. git inti command
This command is used to initialize git in a new or existing directory. It creates a hidden directory .git, which is responsible for all the git operations and history control of the directory.
git init <directory name>
If we specify the directory name, it will initialize the git in that directory. If the directory name is omitted, it will initialize git in the current working directory.
The above command will initialize git in the current directory.
2. git clone
With the git clone command, we can download the existing remote repository into our local system from sources like GitHub.
git clone <link of the repository>
3. git add
After making the changes in the files, we can add all those changes to git by using the git add command. The git add command takes a snapshot of the changes before we make the final commit. Using the git add command, we can add all the changes made over the project or just add the specific file or directory changes.
To add a single file
git add <file name>
To add all files
git add . Or git add -A
4. git commit
git commit is one of the most used git commands. After adding the changes using the commit command, we can save all the changes with a message.
git commit -m "commit message"
5. git branch
Using the git branch command, we can create, list, and delete branches of a git project. Branches represent the developers connected to the same git project and help developers to work on the same project.
a. Create a new branch
git branch <branch name>
This command will create a branch by name guest1 locally.
b. View all branches
To view all the branches, we can use the following commands:
git branch or git branch –list
c. Delete a branch
To delete a branch, we can use the branch command with the -d flag.
6 . git checkout
To switch between branches, we can use the git checkout command. This command allows us to move from one branch to another.
git checkout <name of the branch>
Before checking out to another branch, here are some points you should keep in mind:
- The changes must be added and committed before switching to another branch.
- The branch to move must also exist locally.
We can also use the checkout command to create and move to a new branch.
git checkout -b <branch name>
Example The -b flag in the above command stands for branch.
7. git status
To get all the information about the current working branch, we can use the git status command.
The status command returns the following information:
- The current pending updation status.
- The current pending commit, push and pull status.
- Staged files, unstaged files and untracked status.
- The file creation, modification, and deletion status.
The git status command comes useful to see all the details before we switch to another branch.
8. git remote
To upload the locally git project on a remote server like Github, we first need to connect our local project to the remote server. To connect the local repository to a remote server repository, we can use the git remote command.
git remote add origin <remote_server_link>
Note: Github now prefers to use the name main rather than master for the root or main branch.
If your main branch name is master, you can rename it using the git branch -b command. From the master branch, run the following command.
git branch -m main
It will change the name of the master branch to main.
9. git push
After making the changes using the commit command to upload the project directory or changes on the remote server like GitHub, we can use the git push command.
Git push <remote> <branch-name> Or git push origin <branch-name>
10. git pull
When multiple developers work on a single project, and everyone push the new commit from their sides, there to get the latest or updated repo from the remote repo, we can use the git pull command. The git pull command gets the updated repo from the remote server and merges it to our local setup repo.
git pull <remote>
The pull command is a combination of git fetch and git merge commands.
11. git revert
To undo a git commit, we can use the git revert command. Every time a commit is made, a unique commit id gets associated with that commit. To see all the commit ids, we can use the
git log –oneline
git revert <commit id>
Note: After executing the git revert command, the vi editor will open to set the new revert commit message. To quit from the vi editor, you can press ESC+:w+q and hit enter.
12. git diff
To see all the differences in the files before adding them using the add command, we can use the git diff command .
To see the file difference between the two branches, we can use the git diff [first_branch_name] [second_branch_name] command.
13. git reset
Similar to git revert, the git reset command can undo the already made commits. Using git reset, we can get back to a previous commit git project. The main difference between the git reset command, and the git revert command is that git revert makes a new commit by reverting to the specified old commit. But the git reset command gets back to the specified old commit and deletes all the next commits.
git reset <commit_id>
14. git rm
With the help of the git rm command, we can delete or remove files and add them to the git stack. The git rm command not only deletes the file locally, but it adds the deletion operation into the git history.
git rm <file>
15. git log command
The git log command lists the history of commits.
16. git show command
To see all the metadata and the content change done in a specific commit, we can use the git show command.
git show <commit_id>
17. git merge command
When multiple branches make commit from their side, we can use the merge command to merge all those commits or changes into one unified branch.
git merge <branch_name>
18. git stash command
With the help of the git stash command, we can save our uncommitted changes for later use. After saving the uncommitted changes using the stash command, we can perform commit, switch branch, and any other operation. But when we want to revert back to one of the uncommitted saves, we can use the stash saved uncommitted stag.
git stash save
To list out all the stash lists, we can use the
git stash list
19. git fetch command
Similar to the git pull command, we can use the git fetch command to download one or more than one remote repository in our local repository. The difference between git pull and git fetch is that the git pull command downloads and merges the changes in our local repository, whereas the git fetch command will only download the repository.
git fetch <remote>
20. git config command
With the help of the git config command, we can configure the user for the local and global setup of the git. We can also use the config command to see the current user configuration of the repository.
To set the current local username of the repo
git config –local user.name "Username"
To see the current local username of the repo
git config –local user.name
To see and set the current username and email of the global repo
git config –global user.name git config –global user.name "Username" git config –global user.email "firstname.lastname@example.org"
21. git grep command
The git grep command helps us to find the files that match the specified pattern.
git grep -n <pattern>
Note: The -n prefix stands for the line number for the matching lines.
22. git archive command
To compress a repository to a tar, tgz, zip, or tar.gz file, we can use the git archive command.
git archive –output <./file_name.format –format> –<fomat_name> HEAD
23. git gui command
Most of the time, when we work with git, we use CLI (Command Line Interface), but git also provides a TCL/Tk-based graphical interface for better visualization of the git operation. To use this graphical git interface, we can use the
After executing the git command, you will see a similar graphical interface:
24. git gc command
gc in the git gc command stands for garbage collection. Using this command, we can clean the mess, garbage collection, unwanted or inaccessible resources from the current repository.
It is a utility tool git command that comes useful to visualize the repository or a specific commit.
26. git –version command
To see your current git version, you can use the git –version command.
27. git –help command
To list the top git commands and see the official documentation of the commands, you can use the git help command.
To see the official documentation of a specific command, we can put the command name after –help in the git command.
git –help <command_name>
28. git --exec-path command
It is one of the basic Git commands. To know the path where your git program is installed, you can use the git --exec-path command.
29. git bisect command
When multiple developers work on the same project, it increases the number of commits as well as the complexity to find the bug among those commits. To tackle this problem, Git provides the git bisect command for debugging the bad revision or commit made by you and other developers, so you do not have to manually look for every commit.
30. git clean command
To remove all the files of a repository that is untracked by the git, we can use the git clean command. Untracked files are those files in the repository that are unknown to the git and have not been staged. The git clean command is generally used to remove the build files.
git clean -i
The -i flag stands for interactive mode, which means it will log the details of the cleaning process. We can also remove the ignored file by setting the -x option.
git clean -x
31. git shortlog command
The git shortlog command comes very useful when you want to see all the numbers of commits and commit messages on a branch.
32. git annotate command
The git annotate command returns the information about every line of the file from the commit when the line was introduced in the file.
git annotate <file_name>
33. git blame command
The blame command is similar to the annotate command. It also provides information about the file, such as the last modified date and revision. The only difference between git blame and git annotate is the output format. Git also prefers to use the git blame command instead of git annotate. The only reason why git annotate is still in use is because of the backward compatibility with older repos.
git blame <file>
34. git count-objects command
To know the number of unpacked files and the total space they are occupying, we can use the git count-objects command.
35. git show-branch command
To see all the available branches on repo and the commits made by those branches, we can use the git show-branch command.
git show-branch -a
-a option is to show all the branches.
36. whatchanged command
The whatchanged git command works similarly to the git log command. It also shows the commit details along with the difference that each commit introduces in the repo.
37. git cherry command
With the git cherry command, we can find out all the commits that yet can be applied to the mainstream.
38. git cherry-pick command
git cherry-pick is a very powerful git command. Using this command, we can select a commit from one branch and apply it to some other branch. To use the Git command, you first need to switch to the branch where you want to apply the commit then execute the following command.
git cherry-pick <commit-hash-id>
To see commit-hash-id, you can use the
git log –oneline
39. git rebase command
When we use the git merge command, it merges the changes from one branch into the current branch. The merge command is generally used to put the final commit of one branch to the main branch. The merge command put the changes in the forward direction, but with the git rebase command, we can take the commits of the current branch and apply them to the head or top of the specified branch.
git rebase <specified_branch>
40. git describe command
The describe command shows the tag that is reachable from the latest commit.
git describe –all
41. git maintenance command
If your git commands are running slow for the repository, you can run the git maintenance command to optimize the repository data and speed up the overall commands.
git maintenance run
42. git mv command
To move or rename a local repo file using git, we can use the git mv command.
To move the file
git mv <source_file> <destination_directory>
To rename the file
git mv <source_file> <renamed_file>
43. git difftool command
To see the changes or differences between two commits, we can use the git difftool command.
git difftool commit1-hash-id commit2-hash-id
45. git fsck command
The git fsck command is only used to check the connectivity and validity of the object database (.git/objects directory).
46. git prune command
The git prune command is a utility git command. It can remove the lost, unreachable, or orphaned git objects database (that are present in the .git/objects directory).
47. git submodules command
With the submodule git command, we can add someone’s repository into our own repository. This command is very useful when one project repository has dependencies on another repository.
git submodule add <repo_url>
There are hundreds of git commands, but here, we have provided only a few of them, mostly basic Git commands. If you use Git for your project, then these are the most common Git commands you will use frequently. When you use Git, you do not need to learn each command. Just learn the basic ones, and those are enough to serve your purpose. The most common git commands are:
To set a repository:
To make changes to the files:
To check the history:
To grow and mark the changes or history:
To work in collaboration:
Ensure adding git before each of these Git commands.
People are also reading: