In this post, I will show you some common Git commands, how to use them and explain exactly what they do. By the end of this post, you will know “How to Git”.
What is Git?
First, let’s learn what GIT is. What benefits as a developer do you get from using GIT? And learn why GIT will soon become your favourite tool, especially when it gets you out of a sticky situation.
GIT is a free and open-source version control system.
Great, but what does that mean?
Version control or source control is the practice of tracking and managing changes to software code. Using a version control system, not only can you track changes to code within a team of developers, but the system will control and merge the changes if multiple team members are working on the same files.
Get started with Git
To get started using Git, first you will need to install it on your machine. You can download Git from the official website.
Once you have Git install on your local machine, using the terminal, navigate into a directory you want tracked by Git.
Next, run the Git init command. This will initialize a new Git repository within the directory.
From this moment on, all changes will be tracked by Git.
Common Git Commands
The Git clone commands is used for downloading existing source code from the remote repository, such as Github. using the Git clone command, Git will make an identical copy of the latest code of a project on your computer.
The command below will clone a remote repository into the current working directory.
git clone <https://repo-link-here> ./
Git commit is the most used Git command.
What does Git commit do? Git commit makes a copy or checkpoint of the current version of code. When you commit your changes, you are saving that exact version at the time, allowing you to return to the version at a later date.
When making a commit, you need to add a short message. This message should be clear about what changes have taken place in this version of the code, so it’s easier for yourself or other developers to know what version it is.
This version of the source code is only saved on your machine, so you would need to use the Git push command to save the current version of the source code on the remote repository, for team members to later pull (getting your version of the code on their machine)
The Git pull command is used to get updates from the remote repository. This command combines git fetch and git merge.
If a team member has made committed changes to the remote repo, say deleting a file, when you run the Git pull command, the file will then be removed from your local repo.
The Git status command is used to print information about the current branch.
Using the Git status command you can get information on the following:
- Files which have been modified, created or deleted
- Files which are staged, unstaged and untracked
- The status of the current branch (is it up to date)
- If there is anything to commit, push or pull
The command can show files that have been modified, files which aren’t being tracked and files that have been deleted.
A branch is useful so many developers can work on the same project at the same time, committing changes to the remote repo regularly to save work without the need to worry about conflicts in code at this stage.
Conflicts will likely occur when all branches are merged together.
To make a branch in Git you will need to run:
git branch <branch-name>
To push a branch to the remote repo use:
git push -u <remote> <branch-name>