Tag Archives: git

Every thing you need to know to master git, Part 7, git branch

It quite often happens that a software product is on a git server and developers add new features to the product. To make the job easier, developers usually create branches beside the master branch (which is the default branch) and work on their branch and once the changes are confirmed and tested, they will be merged to the master branch. So let's get started:

ok now let's create two branches, one for developer1 and one for developer2,

let's make some work on the first branches:

and some changes in the second branch:

now you should see this in git gui or if you type:

Now in the git gui, go to merge>local merge and choose b1, you will get the following error because of the merge conflict:

so call the following to solve the conflict:

choose C and save and then:


Working with branches in nutshell:

Creating a branch:

Track a Remote Branch:
If you have already created a local branch and you want to track it with remote branch:

Push a branch and automatically set tracking.  you can use the -u flag (upstream) when you make your first push:


To view the branches in a Git repository:

To delete a branch:


Ref [1]

Every thing you need to know to master git, Part 6, git stash

Sometimes you have made some changes into your local copy and you don't want to make a commit because your code doesn't compile, but something came up and you should change your branch for instance and you will lose your local changes. In this situation, you can stash your changes and save your changes and change your branch for instance.

Let's have a complete example:

now we make some change:

now let's stash changes:

we can list and drop changes by:


Every thing you need to know to master git, Part 5, git cherry-pick

To illustrate what is cherry-pick let's imagine  the following situation: you and another developer start working on project, you will both make some changes, but after a while the other developer find a bug in the code and you are interested in to only apply that hot-fix without taking other changes from another developer, in this situation you should do a cherry pick, so let's do it:

and you continue with something like:

The other developer checks out the repository and will start working and make the second commit:

and in the third commit, he fixes the problem with A.txt so he would something like this:

And he may continue his work with another commit:

Now you are only interested in the third commit and you don't want to add other files to your local copy so you have to cherry pick the third commit, so all you have to do is fetch all changes, find the corresponding SHA1 value for the third commit and apply the cherry pick.

And now the file A.txt in repose1 should be same as the file in repose2 while C.txt and D.txt are not added to repose1.




Every thing you need to know to master git, Part 4, git merge, git diff with GUI tools

Before following this post, make sure you have already set up everything from my other post regarding the configuration of git environment.


Diff command will allow you to compare two different commits, let's set up a repository:

Now in the second repository, we make some changes:

now let's back to the first repository and see the changes:

now you should see all the difference in meld:

To find diff between current and previous commit:

As of Git 1.8.5, @ is an alias for HEAD, so you can use:

If you want to know the diff between head and any commit you can use:

You can add -t to select your difftool such kdiff3, meld, etc.
To see the diff with gitk, first go to Edit>Preference and set the externall difftool.
Now click on your revision, then right click on the revision that you want to compare to and click on Diff this-> selected. Now on the bottom window right click on ny of changed file and select External diff

Merge and conflict

now let's merge all the changes. If you make some changes on dii=fferent files you will do fast-forward. That means you only incorporate new changes and everything is ok afterward. But if you made some changes on the same file in same lines then you have a merge conflict and you should do a three-way merge.

To show you the point lets make some changes in the first repository in the file A.txt.

now if you call git merge, you will get a merge conflict, to solve this call the following:

and you will get this in meld:

and this in kdiff:

|                MERGED               |

LOCAL: Your side of the conflict - ie, your branch (HEAD) that will contain the results of the merge
REMOTE: The remote side of the conflict - the branch you are merging into HEAD.
BASE: The common ancestor of both local and remote. useful for feeding into a three-way merge tool
MERGED: The result of the automatic merge.

go through the conflict and choose the side (A, B or C) and see the merged file in the window below and save and close.

Everything you need to know to master git, Part 3 (git checkout and git clean)

Cleaning up git environment and rollbacking local changes

Once you started working on your local copy you might mess everything and need to clean up or roll back changes for some file, you can use git checkout and git clean

to rollback changes for a particular file:

to rollback changes for the entire local copy while keeping added files:


if you just want to remove the file and directories that have been added but not staged:

Remove untracked directories in addition to untracked files.

Don’t actually remove anything, just show what would be done.

Show what would be done and clean files interactively. See “Interactive mode” for details.

This will create a new branch and switch to that:


Everything you need to know to master git, Part 2, git revert, reset amend

How to undo a commit in Git when no one has pulled your changes from the repository? git reset

How to push to a non-bare Git repository?


git reset will do this job for you, it has two option, hard and soft,

git reset --hard/ --soft <SOME-COMMIT>

1)Hard: this option will reset the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded.

2)Soft: this option will leave the unstaged files in the working tree.


A Complete setup


some important note:

git add -A is equivalent to

Let's continue our work:


The git reflog command records a chronological history of everything you have done in your local repository. Its full output might look like this:

C.txt and D.txt will remain (soft)

C.txt and D.txt will be deleted.

To see which files have been added and status:

Finally lets' push changes by:


How to undo a push in Git when someone might have pulled your changes from the repository? git revert

If you’ve already pushed your changes and someone else pulled in those changes, you should not use git reset to undo changes, use git revert instead.


Let's follow our work:


How to edit the git comment? git commit --amend -m "updated"

This will fix your most recent commit message



Pushing into the wrong branch

A very practical situation for the above command is pushing into the wrong branch,


Everything you need to know to master git, Part 1, setting up git environment, GUI, mergetool, difftool

Installing prerequisite

Configuring Git Environment

First config your environment and set a proper merge/ diff tool.  The settings are in /home/<username>/.gitconfig

All these setting can be set or viewed via terminal, i.e

To view all settings

To view current settings of mergetool:

To set them

All available options for setting a merge tools can be listed by:

here we config it for meld, kdiff3 and vim.


Add the following to your .gitconfig file



User name, Email and editor


Adding Alias


SSH key

and upload your public key


into your GitHub account

Push into github without password using ssh-key

Do the following:

For Example if your repository is:

Run the following: