I forgot to commit my changes on branch feature-23
before changing to feature-24
and making different changes on there. Now both branches show all the changes I made between both of the branches.
How can I separate the changes so each branch only shows the original changes I made to them.
CodePudding user response:
first step, save your modifications, even if your changes are mixed together :
git add -u # -u will add all tracked files,
# another option is -a (for 'all')
# or explicitly name the files/folders :
# git add file1 file2 dir3 ...
git commit # create a commit
git branch tmp # create a tmp branch at that commit location
git reset HEAD~ # return one commit behind
# important note: *not* --hard
Now the content of your files is accessible from branch tmp
.
If the files to commit on feature-23
are separate from the files on feature-24
, the actions are easy :
# suppose you currently are on branch feature-24 :
git add relevant-file1 relevant-file2
git commit # create the commit for feature-24
# now go to the other branch :
git checkout feature-23
git add relevant-file3 relevant-file4
git commit # create commit for feature-23
If some changes are on the same file (part of the changes should go to feeature-23, other part to feature-24) and are not too intertwined (e.g: are clearly separated chunks in the diff), you can use git add -p
:
git add -p relevant-file1
# an interactive session will ask you to add or ignore each chunk in the diff
git add -p relevant-file2
git add relevant-file3
git commit
# then switch to the other branch, and proceed
There also is a graphical tool that ships with standard git : git gui
.
Its GUI is a bit clunky, but it offers quite a load of useful features. You will have a graphical presentation of what is on your disk (the worktree), what is staged to be committed (the index), and a file by file diff view, where you can right click and add/remove complete chunks or individual lines.
CodePudding user response:
There are two situation:
If the changes are in branch feature-23 and feature-24 are in difference files. it will be easier to solve
You need to checkout to each branch and commit related files
If the changes between two branches are in the same file, you need commit partial file, like here