Useful GIT commands for everyday use

In this post, I share some useful GIT commands for everyday use. After years of using GIT, I’ve found useful commands to make my developer’s life easier. This is the list of the most frequently used in my case.

I’ve created the Github demo repo here, containing all of these commands. I will try to keep this post up to date when other useful commands came out. If you have another useful command I missed, your comments are welcome!

Clone a repository

How to clone a repo creating the folder with a different name (eg: git_commands instead of the default name git-commands)

git clone gitcommands
cd gitcommands

To clone a single branch from a repository (useful only when the repository is very large and contains binary files that slow down the performance of the repository) [Since Git 1.7.10]

git clone --single-branch --branch my-branch

Cleaning a repository

Discard all changes to modified files in your repo. Only staged files (exclude untracked files)

git reset --hard HEAD

Discard all untracked files (You can check first which files would be removed with git clean -n)

git clean -f

Restore modified files (not yet committed)

git checkout filename

Unstage all files (remove them after running git add) or, optionally, unstage some specific file(s) adding -- path/to/file

git reset [-- path/to/file]

Revert the last commit (locally, before pushing to remote). This will remove the last commit. All committed files will be returned to stage (pre-commit). You can use it, again and again, to go back one step more.

git reset --soft HEAD~1

In case you want to revert the very first commit in your repository, use this command:

git update-ref -d HEAD

Pulling changes

Pull changes into a forked repo from a branch in another source repo:

git pull source-branch

Another way more consistent is to save the repo url as a remote source with alias (upstream in this case).

git remote add upstream
git fetch upstream

In this way, you can use the source repo for any other operation referencing branches as, for example, upstream/my-branch. For example, pulling remote changes and then replay your commits on top of that branch

git rebase upstream/my-branch

Merging changes

If you get a merge conflict and you can go back to pre-merge state (abort), removing merged files and conflicting versions as well:

git merge --abort

If your want to resolve conflicts using the remote version (from the branch you are merging) of that files, abort the merge and then:

git merge -Xtheirs [branch-name]

In the opposite case, you can merge using the local version of the conflicted files using:

git merge -Xours [branch-name]


How to push local to the remote (master) repository for the first time (from a locally-created repo to an empty repo)

git remote add origin
git push -u origin master

The -u parameter is to setup this origin as the upstream. So any next call to git push or git pull does not need to specify origin/branch when working on this branch.


Reset your repo to a specific commit (all changes after this commit will be removed). You can see the commit hashes using git log

git reset --hard e8fe18df3a8ff8220b9158f53a37dc163f45bc67

Forcing a remote repo to match the local repo (also for branches)

git push -force origin master

Working with branches

List current branches

git branch

Create a new branch in your local repo and switch to that branch

git checkout -b new-branch

Also, the first push of a new branch to a remote should be (-u is equivalent to the option --set-upstream )

git push -u origin new-branch

In this way, you can push and set up to track the remote branch origin/new-branch

Checkout/Switch to an existing branch

git checkout test-branch

Deleting Branches

Delete a branch locally and remotely (the -r option is to delete it remotely)

git branch -d -r my-branch

Also if you already deleted the branch locally, without the -r option:

git push origin --delete my-branch

Also, if you still see the branch in the remote repository, supposedly deleted, you need to prune the origin:

git remote prune origin

After deleting the branch you can sync your local branches using

git fetch -p

Remove all branches already removed on the remote repository (list all branches marked as ‘gone’ and remove them using git branch -d:

git checkout master
git branch -vv | grep ‘: gone]’ | awk ‘{print $1}’ | xargs git branch -d




Programming from the past century. Worked with many programming languages and software development tools for desktop, web and mobile.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Python lists & Sets

How to Become A Front-end Developer in 2022: All Details

Arena Mode has been Updated,The New version of gameplay instructions

What I Wish I Knew Before I Learned to Code

Retrieving AWS EC2 Instance metadata &using metadata in Scripts.

Why Flutter is not your choice for building the best app in the world?

Docker — Basic Introduction.

Operating Model Considerations for Cloud Governance Success

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Francisco Igor

Francisco Igor

Programming from the past century. Worked with many programming languages and software development tools for desktop, web and mobile.

More from Medium