List of life saving git commands

At work we use git and those below commands do really save my day,


Git config

git config --global "Tugrul ASLAN"
git config --global
git config --global core.editor "'C:/Program Files (x86)/Sublime Text 2/sublime_text.exe' -multiInst -nosession"


$ git config --global
Tugrul ASLAN
$ git config --global

Reverting local file back to original version in a different branch

I hear ya that the situation sounds weird, but it happens. Assume that y ou have a branch called child, which has been derived from the master branch. So you work on a file, but you’re willing to change the file back to its version in the remote master. so the command is

git checkout origin/master FILEPATHANDNAME

Reverting a file back to its previous version

In this case assume that you’d like to restore a file back to its previous commit.

–> list commit history

git log FILE


–>revert the file




–>commit and push your changes



Reverting/Aborting local commit

Imagine that you’ve mistakenly committed all files instead of ones you really intended to, you don’t want a few of files to be pushed to the upstream, the issue the below command to revert it

Windows Equivalence

git reset HEAD~1

Unix Equivalence 

git reset HEAD^


Adding new files ,commiting and pushing 

git add index.jsp ==> will add only one file

git add . ==> will add all newly established files not existing and altered ones,

git commit -m “your commit message”

git push origin REMOTENAME

Showing file remote history

To see the version and differences the command is


git diff e063d1bf1a235646da0c50b07e6c5865ca7f45b7 bin/custom/xx/xxxfacades/src/com/xxx/ebebek/facades/populators/




The lines appear in red color corresponds to file’s remote history, and the green lines indicates the current local version

Discarding unnecessary  files

the below files are undemanding and we have implemented changes somewhere else, so now we’ll eliminate and discard



git checkout -- .

in this command checkout — corresponds to discard a file that you don’t want it to be pushed to an upstream, and dot corresponds to all files, if you are not willing to explicitly imply every file one by one

the outcome



When you are behind the commits


Which indicates to the situation that your current workspace is behind the updates

Simply issue

git pull

to acquire the changes

Deleting a branch

if you wanna delete a local branch

git branch -d BRANCHNAME

Deleting the remote branch

git push origin :BRANCHNAME

Git init a new repo

This situation regards to that imagine there is no git relation in a folder, but you are willing to bind git repo into this folder

first initialize git

git init

then bind the source code

git remote add origin


Reverting local commits

If  your local is somehow ahead and you want to revert it you simply reset it hard back to remote’s status

git fetch
git reset --hard origin/REMOTEBRANCHNAME
git clean -f -d

Showing committed files that your will push to the remote

git diff --stat --patch origin REMOTEBRANCHNAME


Pulling different branch into local master

Assume that in your local master, you want to merge a different remote into that, so the command is merging

Fetching a remote branch into local which does not exist, for the first time

Assume that there is a branch in remote called HYB-QA, but it does not exist in your workspace, to fetch the branch issue

git fetch origin HYB-QA:HYB-QA  ==>remote:local

git checkout HYB-QA

then now I am assuming that you intend to merge the changes into master branch from HYB-QA simply issue the command

git merge HYB-QA

this will merge the differnces into the master branch

Dealing with conflicts

conflict is simply a situation in which git either decides to auto merge or indicates an alert that it cannot auto merge the files.

Let’s observe how merging mechanishm works merge

Assume that there is a file called index.html,

->user tugrul added abc in the #1 line, and committed

->user john fetched the same file added dec in line #2

then git will automatically auto merge it because it know changes were applied in different lines.

2. Conflict merge

This is the painful one because git cannot figure out what lines were changed, let’us assume the same users in the same file but the actions as follows;

-> user tugrul deleted the line #1, and committed and pushed his code

-> user john fetched the code and altered #1 added some new value then pushed

-> user tugrul re fetched the same file and he will experience a conflict,

in this scenario git will not decide what to do, because line one was previously deleted, then accordingly it were altered with a new line

so in this situation git will mark the file as

<<<<<<< HEAD
 >>>>>>> 4e2b407f501b68f8588aa645acafffa0224b9b78

here you see that git cannot decide which is correct, <<<<<<< Head indicates its previous version, ====== is the seperator and the next following line is the newly added one. So git will ask you to change the line here, simply the action for you assuming that you say dec one is corrent

then delete >>>>>>> 4e2b407f501b68f8588aa645acafffa0224b9b78 ======== and <<<<<<<<<<<< HEAD lines, simply delete everything but the line you wanna keep in this case it is dec, then save it

your following objectives are to add any files newly fetched if there is any and commit your changes, then you’ll see the git command line will be turned out to be printing “your current branch name” than “merge/MERGING”

Restoring uncommited files

Assuming that you have changed index.jsp and user.jsp files, and you want to commit index.jsp, but user.jsp. So in this case the user.jsp file will not be committed just restored back to its previous version

git checkout user.jsp

Revert the remote branch to a previous push/commit

I’ll explain the story with a real situation happened at work. So the branch HYB-1 with commit “5ebde4e auto mergeMerge branch ‘HYB-1′” has wrong files and we wanted to restore it back to the commit “55c075e vouchers, 404 page geliştirmesi yapıldı” one which has been pushed by one of developers Can.


In the above situation first get the hash code of the commit you want the remote branch to restore to. In this situation I get the hash code of Can’s commit


First find the hash code of the push you want to restore to

git reset --hard 55c075ef0c619d1a885386c1b395c6e3e32f85f3

then force push it back to the remote, in git’s internals, we do the changes in the local then push it to the remote so issue the below command

git push -f HYB-1

then you’ll see HYB-1 restored from  “5ebde4e”  to “55c075e” the changes are immediately applied on git.


Reset and revert the local branch to remote branch’s commit

if your local branch has conflicts, and ahead of your remote branch. Now you are willing to bring it back to remote branch’s state.

Retrieve the updated remote branch into the local branch

git fetch REMOTENAME

reset the current local to the remote version

git reset --hard origin/REMOTENAME

then clean the altered remaining files

git clean -f -d

Leave a Reply

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