Essential Day To Day GIT Commands

As the title suggests, this post will cover all the essential day to day GIT commands that you will need on a daily basis.


Tired of looking around for the essential GIT commands? Well look no further. Go ahead and bookmark this page. Here we will cover all the essential GIT commands that you would need on a day to day basis.

We are not concerned with those rare extreme scenarios. No, this is going to be just the GIT list you need.

DecisionMentor app

Completely new to GIT? Checkout: Introduction to Version Control System

How To Add GIT Username And Password Globally

If you are working on your personal machine, it will be easier to save your git account username and password globally on the computer. This way you no longer have to type in credentials every time you wish to clone or pull a private repository.

You can set your username and password globally like this:

git config --global "your username"

git config --global user.password "your password"

How To Turn Your Project Into A GIT Repository

Say you have an existing project my_project and you want to enable GIT version control on this project. Basically you are trying to initialize a GIT repository here and this is quite easy.

Open the terminal inside your project folder and type the following commands:

git init

You should receive a message similar to this:

Initialized empty Git repository in [your project path]/.git/

And if you take a look inside the folder, you will see that a new folder .git has been added to it which contains all the necessary files and folders for the version control to work on this project.

How To Add A Remote GIT Url

The command to set a remote url to git repository is:

git remote add <remote-name> [remote-url]

So, if you want to add a remote origin, it would look like this:

git remote add origin [remote-url]

How To Change/Update An Existing Remote GIT Url

You can change or update a previously set git url with the command below:

git remote set-url <remote-name> <remote-url>

If your remote name is origin:

git remote set-url origin <remote-url>

How To Checkout A Remote GIT Branch Locally And Push Changes To The Remote Branch

Let’s say you have a branch r_branch_1 on the remote that you want to add changes from your local branch. Let’s assume that your remote branch is named upstream.

First, checkout the remote branch locally.

git checkout -b r_branch_1 upstream/r_branch_1

The command above creates and checks out the local branch named same as the remote branch.

Next, add changes to this branch. When you are ready, push the changes to remote.

git push origin

How To Copy Commits From One Branch Into Another Branch?

Say you are on branch_A and you want to copy some commits of this into branch_B. Follow the steps below for this purpose:

Check out the branch that has the commits which needs to be copied over.

git checkout branch_A

Now, get the list of commits that you have made on this branch via the git log command.

git log --oneline

Say you have a new commit on this branch with id 81d3589.

81d3589 (HEAD -> branch_A) added file2
a7f1db5 (master, branch_B) initial File

Keep a note of this commit id and now move onto your second branch.

git checkout branch_B

From this branch, now we will use the cherry-pick command to copy the changes from branch_A.

git cherry-pick 81d3589

Simple as that. you have the changes now copied to branch_B.

Checkout Merge Request Locally

So you want to checkout a merge request from your colleague locally before merging it to master. Here’s just the right command for that purpose:

git fetch REMOTE_NAME merge-requests/REQUESTID/head:BRANCHNAME

Replace REMOTE_NAME with your remote branch name. It could be origin, upstream or any other name you have set. REQUESTID is the merge request id in github or gitlab and BRANCHNAME is the name of the branch.

How To Rename Git Remote Name

You can rename existing remote name of the git repository with this command:

git remote rename <old-name> <new-name>

For example, if you want to rename origin as upstream you say:

git remote rename origin upstream

More to come soon…