Git is a Version Control System (VCS), it allows you to keep tracks of changes in your files and simplifies collaboration when working with a group of developers.
Even if there’s a ton of Git graphical user interfaces out in the market, every developer should master some starter’s pack commands. They’re easier to use and can be a lifesaver when accessing a user interface is not an option.
The following list enumerates the must-know commands that you’ll need to keep close.
git clone
Before starting to work, you’ll have run this command to download all the project files from the remote server and have them on your local computer.
The remote server can be GitHub, GitLab, Bitbucket or another service provider.
Usually, the project located on the remote server is referred to as “origin”, while the copy you make is the “local”.
> git clone URL
URL is the link to where the original project is stored on the remote server.
git branch
In Git terminology, a branch is a new work line issued from the master project. In other words, when you intend to work on a new feature, creating a branch allows you to start your developments in an isolated directory, without impacting the latest stable version of the project.
Once the branch is tested, you can merge it to make it a part of the original project.
> git branch
This is the minimalist form of the command, it allows to list all the available branches in the project its run upon.
The command can be extended with other attributes:
> git branch <new-branch-name>
This variation allows creating a new branch. The branch name is the reference that allows you to switch on it if you’re on another branch, or to delete it if you want to.
> git branch -d <branch-name>
When adding -d attribute after git branch, it signifies that you intend to delete a branch identified by a name.
git checkout
The checkout command allows switching between branches.
Imagine a scenario where you’re already working on a feature in a branch, but an emergency came and it requires a hotfix. In that case, you’ll switch to another branch, create the hotfix then switch back to your initial branch.
> git checkout <branch-name>
Called like that, the selected branch becomes your current line of development. So Git will manage to save the work you were doing in the old branch, then update the project files to match the selected branch.
Another variation of this command is:
> git checkout -b <new-branch-name>
Called this way, Git will create a new branch and switch to it. Think of it as a shortcut, assembling git branch and git checkout.
git status
This command is used to check the status of the files in your directory. It allows you to check which files have been updated since the last commit, along with the files that aren’t being tracked with git.
> git status
git add
Before committing updated to the remote server, you want to start by preparing a “staging area”. That means, to tell Git what to ship in the next commit.
“git add” allows you to add changes to index in your directory. That way, Git will know that you want to record the updates you made in the selected files, so it will record them in the local repository.
> git add
Called this way, all the files in the current directory will be added to the staging area. If you only want to add a specific file or directory, you can add its name to the end of the command.
> git add <file | directory>
git commit
You created your branch, finished working on your new feature and told Git what files you want to save by using “git add”.
Next step is to “commit” the updates you made. Git will apply all the changes you made, so they became a part of the root project on your local repository.
Please note that in Git, a commit in not automatically uploaded to the remote server. So whatever updates you committed, it didn’t leave your local repository. You’ll have to explicitly tell Git to do so when you’ll want to.
When committing, you can add a comment to describe the changes you’re recording.
> git commit -m “write your comment”
Leave a Reply