Removing the git merge commit if no commit has been made to master

Removing the merge commit if no commit has been made to master

$ cd gitlearn/
$ ls
$ git init
Initialized empty Git repository in /Users/jaggija/dev_home/code/open/gitlearn/.git/

Create some text file in master

$ pwd
/Users/jaggija/dev_home/code/open/gitlearn
$ vi readme.txt
$ git add readme.txt 
$ git commit -m "Initial commit"
[master (root-commit) 5202907] Initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 readme.txt

Create a new branch and edit the text file

$ git checkout -b "new branch"
fatal: 'new branch' is not a valid branch name.
$ git checkout -b "newbranch"
Switched to a new branch 'newbranch'
$ git checkout newbranch
Already on 'newbranch'
$ vi readme.txt 
$ git add readme.txt 
$ git commit -m "new branch"
[newbranch 0ec90b8] new branch
 1 file changed, 2 insertions(+)


See the log


$ git log
commit 0ec90b8fd3174fe0ad8d0e538f5c7a162a0b2216
Author: Jagat Singh <jaggija@cba.com.au>
Date:   Mon Jul 21 10:17:32 2014 +1000

    new branch

commit 52029078b1635437aec297321c5d23274267e7d6
Author: Jagat Singh <jaggija@cba.com.au>
Date:   Mon Jul 21 10:16:23 2014 +1000

    Initial commit


Switch to master and do the merge


$ git checkout master
Switched to branch 'master'

$ git merge --no-ff newbranch
Merge made by the 'recursive' strategy.
 readme.txt | 2 ++
 1 file changed, 2 insertions(+)


$ git log
commit 4778269d6e3a8be0a68b904653f349b0252736b0
Merge: 5202907 0ec90b8
Author: Jagat Singh <jaggija@cba.com.au>
Date:   Mon Jul 21 10:19:43 2014 +1000

    Merge branch 'newbranch'

commit 0ec90b8fd3174fe0ad8d0e538f5c7a162a0b2216
Author: Jagat Singh <jaggija@cba.com.au>
Date:   Mon Jul 21 10:17:32 2014 +1000

    new branch

commit 52029078b1635437aec297321c5d23274267e7d6
Author: Jagat Singh <jaggija@cba.com.au>
Date:   Mon Jul 21 10:16:23 2014 +1000


See the graph

$ git log --oneline --graph
*   4778269 Merge branch 'newbranch'
|\  
| * 0ec90b8 new branch
|/  
* 5202907 Initial commit


Lets fix the merge commit

$ git reset --hard 5202907
HEAD is now at 5202907 Initial commit
$ git status
On branch master
nothing to commit, working directory clean

Do fast forward merge

$ git merge --ff-only newbranch
Updating 5202907..0ec90b8
Fast-forward
 readme.txt | 2 ++
 1 file changed, 2 insertions(+)

See the log again

$ git log --oneline --graph
* 0ec90b8 new branch
* 5202907 Initial commit
$ git status
On branch master
nothing to commit, working directory clean
$

Done


No comments:

Post a Comment

Please share your views and comments below.

Thank You.