My scrapbook about anything which I learned or want to remember, Sometime about tech tips, thoughts and rambling. If you find anything useful don't forget to give thumbs-up :)

Breaking

Wednesday, March 25, 2020

Merge Specific commit in Git


While working with Git, sometime we need to merge only specific commit to master or some other git branch, we can achieve it easily by following below commands -
My Git Repo have 2 branch - master and atul
I am making changes into atul and then merging a specific change to master branch

$ git log
9055ed1 (HEAD -> atul, origin/master, origin/HEAD, master) Revert "bad"

First Change:
$ git checkout - atul
$ touch firstcomfile && git add firstcomfile
$ git commit -m 'adding first commit'
$ git push origin atul

Now, my git log is somewhat look like below - 
$ git log
d34bd8a (HEAD -> atul, origin/atul) adding first commit
9055ed1 (origin/master, origin/HEAD, master) Revert "bad"

Second Change:
$ touch seccomfile
$ echo 'making change in first file in second commit' > firstcomfile 
$ git add firstcomfile seccomfile
$ git commit -m 'adding second commit'
$ git push origin atul

Third Change:
$ echo 'making another change in first file in third commit' >> firstcomfile 
$ git add firstcomfile 
$ git commit -m 'adding third commit'
$ git push origin atul

git log is as below - 
$ git log
7527e19 (HEAD -> atul, origin/atul) adding third commit
cd17ced adding second commit
d34bd8a adding first commit
9055ed1 (origin/master, origin/HEAD, master) Revert "bad"


Now, suppose, we want to merge only second commit (cd17ced adding second commit) with master, how to do so, let's see - 

$ git checkout master
$ git cherry-pick  cd17ced
# if there is any merge conflict, resolve them 
$ git push origin master

Now, the git log will is something like below - 
$ git log                 # from master branch
7c3788e (HEAD -> master, origin/master, origin/HEAD) adding second commit
9055ed1 Revert "bad"

Voila!!, Specific commit (cd17ced) is merged with master as a commit (7c3788e). 

Caution: When merging specific commit to directly master, There might be conflict which need to be fix while cherry-picking. 

See you in next post, till then...Happy Learning.....



Like the below page to get the update  
Facebook Page      Facebook Group      Twitter Feed      Telegram Group

No comments:

Post a Comment

Disclaimer

The postings on this site are my own and don't necessarily represent IBM's or other companies positions, strategies or opinions. All content provided on this blog is for informational purposes and knowledge sharing only.
The owner of this blog makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site. The owner will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of his information.