Git is widely used in the software industry and is a vital element of daily development (especially if you’re working with a team). Mastering Git takes time because there are so many different commands to learn. However, some commands are used more frequently than others (some daily). So I’ll go through the ten most used Git commands in this essay, which should be known by every developer.
Note: You must be familiar with the fundamentals of Git in order to comprehend this tutorial.
1. Git clone
Git Clone is a remote repository tool that enables you to access source code (like Github, for example). In other words, Git clone saves an exact copy of the most recent version of a project from a repository to your machine.
There are a few ways to get the source code, but I usually use the clone with HTTP method:
git clone <https://name-of-the-repository-link>
If we want to download a project from Github, for example, all we have to do is click the green button (clone or download), copy the URL from the box, and paste it after the git clone command that I’ve demonstrated right above.
This will copy the project to your local workspace, allowing you to begin working on it.
2. Git branch
In the realm of git, branches are quite significant. Many developers can work simultaneously on the same project with branches. You may set up, list, and delete branches using the git branch command.
Create a new branch:
git branch <branch-name>
This command will make a branch on your computer. Using the following command, push the new branch into the remote repository:
git push -u <remote> <branch-name>
git branch or git branch --list
Deleting a branch:
git branch -d <branch-name>
3. Git checkout
This is also one of Git’s most commonly used commands. To work on a branch, you must first switch to that branch. Git checkout is mostly used to switch between branches. It’s also possible to utilize it to double-check files and commits.
git checkout <name-of-your-branch>
To successfully transition between branches, you must take the following steps:
- Before you switch, the changes in your current branch must be committed or stashed.
- The branch you intend to check out must exist in your local area.
There is also a shortcut command that allows you to simultaneously create and switch to a branch:
git checkout -b <name-of-your-branch>
This command creates a new branch in your local (-b stands for the branch) and checks it out to new as soon as it’s done.
4. Git status
Using the Git status command, we can acquire all of the information we need about the current branch.
We can collect data such as:
- Check to see if the current branch is current.
- Whether anything needs to be committed, pushed, or pulled
- Whether any files have been staged, unstaged, or are untracked
- Whether or not any files have been created, updated, or destroyed
5. Git add
The changes we make to a file when we create, amend, or remove it are made locally and are not reflected in the next commit (unless we change the configurations).
To include the modifications to a file(s) in our next commit, we must use the git add command.
To add a single file:
git add <file>
To add everything at once:
git add -A
When you look at the screenshot in the fourth part, you’ll notice that some of the file names are red, which suggests they’re unstaged files. Your commits will not include the unstaged files.
We’ll need to use git add: to incorporate them.
Important: git add does not update the repository, and the changes are not stored until we use git commit.
6. Git commit
This is arguably the most commonly used command in Git. We want to store our changes after we’ve reached a certain point in the development process (maybe after a specific task or issue).
Git commits serve as a point in the development process when you can go back if necessary.
We must also include a brief statement describing the changes or additions we made to the source code.
git commit -m "commit message"
Important: When you commit to Git, your changes are only saved locally.
7. Git push
After you’ve committed your modifications, the following step is to send them to the remote server. Git push is a command that pushes your commits to a remote repository.
git push <remote> <branch-name>
If your branch is brand new, you’ll also need to upload it using the command below:
git push --set-upstream <remote> <name-of-your-branch>
git push -u origin <branch_name>
It’s important to note that Git push only uploads changes that have been committed.
8. Git pull
To retrieve updates from a remote repository, use the git pull command. This command combines git fetch and git merge, which means that when we use git pull, it fetches updates from a remote repository (git fetch) and applies them to your local repository right away (git merge).
git pull <remote>
This procedure may result in conflicts that you must manually resolve.
9. Git revert
We sometimes need to reverse the adjustments we’ve made. There are a number of ways to undo our modifications locally or remotely (depending on our requirements), but we must use caution when using these commands to avoid inadvertent deletions.
Using git revert to undo our commits is a safer option. To view our commit history, we must first run git log — oneline.
Then all we have to do is enter the hash code next to the commit we want to undo:
git revert 3321844
After that, you’ll get a screen similar to the one below; simply hit shift + q to exit.
The Git revert command will reverse the previous commit, but it will not delete the previous one.
Using git revert has the advantage of not affecting the commit history. This implies that all of your commits, including those that have been reverted, are still visible in your history.
Unless we push them to the remote repo, which is another safety measure, everything happens on our local system. In this sense, it is better to utilize git reverse to unlock our commitments.
10. Git merge
When your branch is ready and all works fine, the last step is to combine it with the parent branch (dev or master). The git merge command is used to do this.
Git merge fuses your branch of feature and all commits are back to the branch (or master) of development. It’s crucial to keep in mind that you must first be on the branch you want to merge with your feature branch.
When you wish to merge your feature branch into the dev branch, for example:
To change to the dev branch first:
git checkout dev
You should update your local dev branch before merging:
Finally, merge your feature branch into the dev branch:
git merge <branch-name>
Hint: Before merging your branches, make sure your dev branch has the most recent version; otherwise, you can end yourself with conflicts or other issues.
These are the ten most-used git commands, which I use on a daily basis. There’s still a lot more to learn about Git, which I’ll cover in subsequent articles.