Skip to content

Daily Use Git Commands For Developers

In this article, I will list all Git commands used by developers in daily work life. Git is a powerful source management system, and very popular. I used TFS for a couple of years, but when I got a chance to use Git, I found that I was missing really a robust, very easy and powerful source management practice.

So I have listed here all git commands:

Initialize Git Repository

To initialize a new Git repository, use this command:

git init

This will create a .git folder in the current folder. On windows, it’s a hidden folder.

Branching

The best practice is to create a new branch for each feature or bug fix. To create a new branch, use below command:

git branch <branchname>

This will create a new branch.

Create a new branch from specific commit:

git branch <branchname> <commit_id>

Create a new branch from a tag

git branch <branchname> <tag>

This will create a new branch from a tag.

To push a branch to remote:

git branch -u origin <branchname>

This will push the branch to the remote repository.

Checkout

After you created a new branch, you need to switch or enter into that branch. For that git offers checkout command:

git checkout <branchname>

After that you can see git will indicate the new branch as the current branch.

Branch and Checkout in one command

Above i showed two separate commands to create branch and then checkout to that branch. We can do that thing in one single command:

git checkout -b <branchname>

Easy one-liner.

Merging

After you completed the work and commit it to your new branch, you need to merge these changes to the original branch. For example, say you created new branch ‘newfeature-branchname’ from the ‘master’ branch, then first you need to switch back to the master branch.

git checkout master

This will make the master branch as an active branch. Now we need to merge the new branch into the active master branch, for that, the command is:

git merge newfeature-branchname

And all your changes will be merged in the new branch.

Stash

Git stash is very useful whenever we have to change the branch without committing the half-completed work in the current branch.

git stash

This command will save all the staged files and make the branch clean, so you can switch to another branch.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *