Before you go, check out these stories!

Hackernoon logoGit Commands That Every Developer Must Know by@mcdenny

Git Commands That Every Developer Must Know

Author profile picture

@mcdennyDenis Oluka

Full-Stack Developer

Git, an open source distributed version control system has been helping developers over a decade now. It is a great tool for tracking source code changes during software development. However, there are a lot of git commands, and this article lists the most important ones that will help your team get work done efficiently.

Git Branching

Switching to a different branch

git checkout 'branch_name'

Creating a new branch and switching to it

git checkout -b 'new_branch_name'

If you want to switch to another branch by losing all the local changes. You should be careful while using this command

git checkout -f 'branch_name'

Deleting a local branch

# Use this if the branch is fully merged to the upstream
git branch -d 'branch_name'

# This will delete the branch regardless of its merge status.
git branch -D 'branch_name'

Deleting a remote branch. Before using this command, you must first confirm the remote branch name with

git branch -a.

git push origin --delete 'remote_branch_name'

Renaming a local branch. Remember, you cannot use this command to rename an empty branch.

git branch -m 'old_name' 'new_name'

Renaming a remote branch

git push origin :'old_name' 'new_name'

Git Committing

Committing local changes

git commit -m "A descriptive commit message"

# Use this to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected.
git commit -a

Undo the last local commit.

Sometimes you can commit changes accidentally then later you realize you haven't added some other feature or you committed unwanted changes. This git command will help you reset the commit without losing your changes.

git reset --soft HEAD^

# Or you can specify with a figure
git reset --soft HEAD~1

Warning: To destroy the commit with all its data, use the same command above with the


git reset --hard HEAD^

Delete / Undo the last remote commit

The commands above are for local changes. Sometimes you realize a mistake after pushing the local changes to the remote branch. These commands can help you get rid of those changes completely. Warning: Make sure you have backed up your local changes.

# Undo the local changes first
git reset --hard HEAD^

# Perform a force git push to affect the remote branch
git push origin -f

Git Remote Repositories

Adding a remote repository URL

git remote add origin 'repository_url'

Adding multiple remote repositories.

In this case, you may have a repository hosted by both GitHub and Bitbucket. You push changes to both repositories. Let's set it up.

Let's rename the GitHub's default origin.

# Rename the origin
git remote rename origin github

Add another remote repository, say, from bitbucket.

git remote add bitbucket 'bitbucket_repository_url'

So, we have now two remotes defined; github and bitbucket. To verify the changes, run git remote command with the


# Verify changes

git remote -v
#> github 'github_repository_url' (fetch)
#> github 'github_repository_url' (push)
#> bitbucket 'bitbucket_repository_url' (fetch)
#> bitbucket 'bitbucket_repository_url' (push)

Now all the remotes have been set. After committing your changes, you will push to both remotes.

# Pushing to GitHub
git push github HEAD

# Pushing to Bitbucket
git push bitbucket HEAD

You can also pull from the remotes

# Pull from GitHub
git pull github 'branch_name'

# Pull from Bitbucket
git pull bitbucket 'branch_name'

Saving data to a different branch

Sometimes, you can make changes in a wrong branch and deleting such changes would be costly. In this case, you can use

git stash
for saving your local changes locally and then pick them later for use.

Stash the changes

git stash

Or you can save your stash with a descriptive name.

# Give a descriptive name

git stash save "My recent changes"
#> Saved working directory and index state On 'branch_name': My recent changes

After saving your stash, you can view the stash list using the


# Stash list

git stash list 
#> stash@{0}: On 'branch_name': My recent changes

Now, all the your changes have been stashed. Use the the above commands for switching to a desired branch where you would like to save your changes then retrieve your changes using the command below.

# Retrieve the changes

git stash pop stash@{0}
#> Dropped stash@{0} (stash_hash_ID)


The commands I have covered in this article may help in smooth collaboration in your team. There are lots of git commands in place. For more advanced commands, visit Git Reference.

Happy Gitting! ๐Ÿ‘จโ€๐Ÿ’ป๐Ÿ˜Š


Join Hacker Noon

Create your free account to unlock your custom reading experience.