- What is Git?
- Features
- Git Commands List
- 1. git inti command
- Example
- 2. git clone
- Example
- 3. git add
- 4. git commit
- Example
- 5. git branch
- a. Create a new branch
- b. View all branches
- Example
- c. Delete a branch
- 6 . git checkout
- Example
- 7. git status
- 8. git remote
- Example
- 9. git push
- 10. git pull
- 11. git revert
- 12. git diff
- Example
- 13. git reset
- Example
- 14. git rm
- Example
- 15. git log command
- Example
- 16. git show command
- Examp le
- 17. git merge command
- Example
- 18. git stash command
- Example
- 19. git fetch command
- Example
- 20. git config command
- To set the current local username of the repo
- Example
- To see the current local username of the repo
- Example
- To see and set the current username and email of the global repo
- Example
- 21. git grep command
- Example
- 22. git archive command
- Example
- 23. git gui command
- Example
- 24. git gc command
- Example
- 25. gitk
- Example
- 26. git –version command
- Example
- 27. git –help command
- Example
- Example
- 28. git --exec-path command
- Command
- Example
- 29. git bisect command
- 30. git clean command
- 31. git shortlog command
- Example
- 32. git annotate command
- Example
- 33. git blame command
- Example
- 34. git count-objects command
- Example
- 35. git show-branch command
- Example
- 36. git
- 36. whatchanged command
- Example
- 37. git cherry command
- 38. git cherry-pick command
- 39. git rebase command
- 40. git describe command
- 41. git maintenance command
- 42. git mv command
- To move the file
- To rename the file
- 43. git difftool command
- 45. git fsck command
- Example
- 46. git prune command
- 47. git submodules command
- Summary
- 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:
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.
Features
- 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.
Example
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.
Example
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
To add all files
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.
Example
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
Example
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:
Example
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.
Example
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.
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.
Example
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.
Example
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.
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.
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.
Example
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
command.
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 .
Example
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.
Example
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.
Example
15. git log command
The git log command lists the history of commits.
Example
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.
Examp le
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.
Example
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.
Example
To list out all the stash lists, we can use the
git stash list
command.
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.
Example
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
Example
To see the current local username of the repo
Example
To see and set the current username and email of the global repo
Example
21. git grep command
The git grep command helps us to find the files that match the specified pattern.
Example
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.
Example
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
git gui
command.
Example
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.
Example
25. gitk
It is a utility tool git command that comes useful to visualize the repository or a specific commit.
Example
26. git –version command
To see your current git version, you can use the git –version command.
Example
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.
Example
To see the official documentation of a specific command, we can put the command name after –help in the git command.
Example
28.
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.
Command
git --exec-path
Example
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.
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.
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.
Example
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.
Example
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.
Example
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.
Example
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.
-a option is to show all the branches.
Example
36.
git
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.
Example
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.
To see commit-hash-id, you can use the
git log –oneline
command.
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.
40. git describe command
The describe command shows the tag that is reachable from the latest commit.
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.
Example
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
To rename the file
43. git difftool command
To see the changes or differences between two commits, we can use the git difftool command.
45. git fsck command
The git fsck command is only used to check the connectivity and validity of the object database (.git/objects directory).
Example
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.
Summary
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:
- init
- clone
To make changes to the files:
- add
- mv
- reset
- rm
To check the history:
- bisect
- grep
- log
- show
- status
To grow and mark the changes or history:
- branch
- checkout
- commit
- diff
- merge
- tag
To work in collaboration:
- push
- pull
- fetch
Ensure adding git before each of these Git commands.
People are also reading:
Leave a Comment on this Post