Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git create commit from diff between two branches

Tags:

git

I have two branches which have very little similar history, but are related to each other.

I want the changes between those two in one git commit.

files have been deleted and created between those patches and I want the patch to reflect that

i.e.: the following stuff will not work:

git diff branch_a branch_b -- > patchfile git checkout branch_b git apply patchfile # deletes and adds are ignored git commit # we miss the deletes 
like image 209
Alexander Oh Avatar asked Jun 26 '13 15:06

Alexander Oh


People also ask

How do I commit between two branches?

In some cases, you may be interested in knowing the commit differences between two branches. In order to see the commit differences between two branches, use the “git log” command and specify the branches that you want to compare.

Can two different branches refer to the same commit?

Yes, two branches can point to the same commits.

How do I compare the differences between two branches in github?

On the Github, go to the Source view of your project. You will see a link named 'Branch List'. Once the page opens you can see a list of all the remote branches. Hit on the Compare button in front of any of the available branches to see the difference between two branches.

What is git diff command?

Comparing changes with git diff Diffing is a function that takes two input data sets and outputs the changes between them. git diff is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files and more.


1 Answers

A simple way to make "the diff from branch_b..branch_a" into a commit is:

  1. create and checkout branch tmp at branch_a (git branch tmp branch_a && git checkout tmp) (or git reset --hard branch_a on an existing branch)
  2. git reset --soft branch_b
  3. git commit

that commit will include all the diff between branch_b and branch_a.


This works because

  • 1. causes the files to reflect branch_a. This is the "end result" you want for the branch
  • 2. “resets the head to branch_b” but “leaves all your changed files [i.e. branch_a head] as "Changes to be committed", as git status would put it.” ←(git reset --soft docs, with this example's branches added)
like image 83
Balog Pal Avatar answered Oct 02 '22 23:10

Balog Pal