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.....
Facebook Page Facebook Group Twitter Feed Telegram Group
No comments:
Post a Comment