Home > Mobile >  Commits on main branch being added to other branches
Commits on main branch being added to other branches

Time:03-28

Let's say there are 4 files A, B, C, and D, of which the files A, B, C are committed on the main branch.

I now create a new branch (let's call it sub-branch), do git checkout sub-branch and make changes, and commit file D only.

But, when I push the changes committed on the file D onto the remote repository(Github) by running git push -u origin sub-branch, even the commits of files A, B, C are added to this branch.

I'd want only the changes committed on file D to be reflected in the newly created sub-branch, how do I accomplish this?

CodePudding user response:

A few options are:

  1. Checkout from remote (assuming the commits haven't been pushed).
  2. Checkout the branch before making changes A, B, C in main, then make change D in your newly created branch.
  3. Checkout from the particular commit before you made changes A, B, C git checkout -b <new_branch_name> <sha1>

CodePudding user response:

However, the commits have already been pushed to the remote main branch.

The new sub-branch will include one commit with only changes for D.

Its parent commit, however, will be from branch main (and the parent commit before that too)

That is what a chain of commits is: a directed acyclic graph (DAG)

  • Related