8 Must-Have Git Commands
In this tutorial we are going to see 8 basic yet fundamental git commands and Git is a must-have skill for developers. Maintaining your development work within a version control system is absolutely necessary to have a collaborative and productive working environment with your colleagues. This guide will quickly start you off in the right direction for contributing to an existing project at your organization.
Git clone creates a copy of the project in your local working environment. You just need to provide a path for the project. This path can be copied from the project main on the hosting service such as GitLab and GitHub.
# clone with HTTPS git clone https://gitlab.com/******* # clone with SSH git clone email@example.com:*******
Once you clone the project to your local machine, you only have the master branch. You should make all the changes on a new branch that can be created using the git branch command.
git branch mybranch
Your branch is the copy of the master branch until you make any changes.
Creating a new branch does not mean that you are working on the new branch. You need to switch to that branch.
git switch mybranch
You are now on the “mybranch” branch, and you can start making changes.
It provides a brief summary of the current status. You will see what branch you are working on. It also shows if you have made any changes or anything to commit.
git status On branch mybranch nothing to commit, working tree clean
When you make changes in the code, the branch you work on becomes different from the master branch. These changes are not visible in the master branch unless you take a series of actions.
The first action is the git add command. This command adds the changes to what is called the staging area.
It is not enough to add your updated files or scripts to the staging area. You also need to “commit” these changes using the git commit command.
The important part of the git commit command is the message part. It briefly explains what has been changed or the purpose of this change.
There is not a strict set of rules to write commit messages. The message should not be lengthy, but it should clearly explain what the change is about. I think you will get used to it as you gain experience using git.
git commit -m "Your message"
- git push
The add and commit methods make the changes in your local git repository. In order to store these changes in a remote branch (i.e., master branch), you first need to push your code.
It is worth mentioning that some IDEs like PyCharm allow for committing and pushing from the user interface. However, you still need to know what each command does.
After your branch is pushed, you will see a link in the terminal that will take you to the hosting service website (e.g., GitHub, GitLab). The link will open a page where you can create a merge request.
A merge request is asking the maintainer of the project to “merge” your code to the master branch. The maintainer will first review your code. If the changes are OK, your code will be merged.
The maintainer might also abort your branch and restore the master branch.
- git pull
The purpose of using a version control system is to maintain a project with many contributors. Thus, while you are working on a task in your local branch, there might be some changes in the remote branch.
The git pull command is used for making your local branch up to date. You should use the git pull command to update your local working directory with the latest files in the remote branch.