Archive for the ‘Git’ Category

Let’s say the git remote repository has the following branches

master
develop
bug-fix-3
bug-fix-4

and your local repository has the following branches

master
develop
bug-fix-1
bug-fix-2
bug-fix-3

As you see the branches bug-fix-1 and bug-fix-2 doesn’t exist on the remote. They have been deleted by someone.
Note: For covering
bug-fix-1 branch was deleted after merging with master
bug-fix-2 branch was deleted without merging as the changes were not required.

Now I will explain how to remove the local branches which are no longer on remote.

How to remove merged local branches which were deleted in remote?

Step 1: git fetch -p
After fetching, remove any remote tracking branches which no longer exist on the remote.
This should ideally remove the branches which were merged with master.

Step 2: git branch

You will see that the merged bug-fix-1 branch is removed from your local repository.
master
develop
bug-fix-2
bug-fix-3

But sometime you will see that some local branches (in this case : bug-fix-2) are still present as they were not merged with master branch.

How to delete unmerged local branches which were deleted in remote?

Step 1: git branch -vv

This step will list all the local branchs with some additional information such as their related upstream/remote branch and latest commit message

master 49a9c07a71 [origin/master: behind 1] Merge branch ‘bug-fix-3’ into ‘master’

develop 877142a45c [origin/develop: behind 4] Test commit message

bug-fix-2 cba6823909 [origin/bug-fix-2 : gone] Bug fix 2 final commit

bug-fix-3 1f0a4ace9e [origin/bug-fix-3: ahead 3] Bug fix 3 final commit

You might have noticed that against bug-fix-2 branch, the additional information says “gone“. This means this has been deleted in the remote. Now lets remove it from local.

Step 2 : git branch -D bug-fix-2
Output : Deleted branch bug-fix-2 (was cba6823909).

This command will delete the unmerged branch from your local repository.

Step 3 : git branch
You will see that the bug-fix-2 branch is removed from your local repository.
master
develop
bug-fix-3

Hope this article helps all of you !

Advertisements

Recently our project was migrated from SVN to Git. Prior to that we were working on a development branch and it was out of sync with the trunk for over 2 months as we were doing a total revamp of the project. Once the migration to git happened, the ‘trunk‘ in SVN became the ‘master‘ branch in git and the ‘development‘ branch in SVN became ‘develop‘ branch in git.

Hence when i tried to merge develop branch onto the master branch in Git, i got a lot conflicts. But i wanted to replace/overwrite the master branch with the develop branch. After going through so many articles in google, finally i found the solution.

Step 1: Checkout the develop branch

git checkout develop

Step 2: Perform a git merge into master

git merge -s ours master

You will see the following result in the console

Merge made by the ‘ours’ strategy.

Note: during a git merge, the “ours” branch refers to the branch you’re merging into:

Step 3:  Checkout the master branch

git checkout master

Step 4: Fast forward changes to master

git merge develop

You will see in the console that it’s fast forwarding because Git does a fast forward when you merge a branch that is ahead of your checked-out branch. Consider the following branch and then merge situation. In this case, the develop branch is ahead of the checked out master branch.

Updating f87abf78..f2d1db5b
Fast-forward
../test.properties| 279 +
../dummy.propteries | 921 +-
….

Step 6: Check git status on master branch

git status

You will see on the console that

On branch master
Your branch is ahead of ‘origin/master’ by 796 commits.
(use “git push” to publish your local commits)
nothing to commit, working tree clean

Step 6: Push the changes

git push

You will see on the console

Counting objects: 1, done.
Writing objects: 100% (1/1), 244 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To http://<repo-path>/<repoName&gt;.git
f87abf78..f2d1db5b master -> master

Step 7: check the logs

git log -1

You will see that now the develop branch has been merged with master branch

commit f2d1db5be45be3fade827237867d21fa6c87ee9f (HEAD -> master, origin/master, origin/HEAD, develop)
Merge: e50da5bf f87abf78
Author: Suhas Saheer Mohammad Sajad
Date: Tue Jul 18 13:05:09 2017 +0400

Merge branch ‘master’ into develop