-1

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.

humzaqureshi
  • 39
  • 2
  • 4
  • 1
    They are not in _both_ branches. As long as you do not commit those changes, they will move along with you if you run checkout. There are a few conditions that git checks to see if _with the modified files_ it's ok to checkout (the _modified_ files have to be the same between `HEAD` and what you want to checkout.... if they are, then git allows the checkout to go on). – eftshift0 Oct 25 '22 at 16:21
  • I would like to commit my changes for feature-23, how can I remove my updates from feature-24 from the feature-23 branch before I commit? – humzaqureshi Oct 25 '22 at 16:23
  • 1
    checkout the branch where you want to commit, do the process to commit.... then, when you checkout next time, those changes you committed won't go with you. – eftshift0 Oct 25 '22 at 16:24

2 Answers2

0

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

Manh Do
  • 111
  • 8
  • 1
    This is not correct. Git allows you to checkout with changes laying around your working tree _as long as the modified files are the same between `HEAD` and where you want to go_. That warning is what you see if that validation fails. – eftshift0 Oct 25 '22 at 17:16
  • 1
    Say you modify file `blah.txt`. if you want to go to `A`, git checks `git diff --name-only HEAD..A`. If `blah.txt` shows up in the list coming out of diff, it will reject the checkout.... however if the file is not there, git allows the checkout to go on (the modified file is not touched). – eftshift0 Oct 25 '22 at 17:24
  • how can I change a file in HEAD? – Manh Do Oct 25 '22 at 17:28
  • By committing? Or checking out? Or `git reset --soft`? Or `git reset --hard`? Of course, that does _not_ modify the commit that is (was) pointed by `HEAD` but moves `HEAD` in different manners. – eftshift0 Oct 25 '22 at 17:31
  • I mean, to check what you say. step 1: change a file in HEAD (do not commit like you said) , step 2: checkout to A branch, and then make the same changes as above HEAD (not commit), step 3: checkout to B branch (if it success, you right) – Manh Do Oct 25 '22 at 17:38
  • I didn't say "you modify in `HEAD`". You just modify it (if you want to imply a value there it would be _"in the working tree"_.... which makes it different from HEAD). – eftshift0 Oct 25 '22 at 17:42
  • i'm sorry, can you describe it with a few commands? – Manh Do Oct 25 '22 at 17:45
  • It's not that complex..... take any project you have in front of you.... choose a place that you want to move to. Run `git diff --name-only HEAD..THAT-PLACE`. Modify a file that is **not** in that list (and that is tracked). Then try to run the checkout and see if git complains. **tip** it won't complain, checkout will happen, file will be just like it was before the checkout – eftshift0 Oct 25 '22 at 17:46
  • I understood the situation you said, but from the begining I want to mention files that git tracked, not unstracked-file – Manh Do Oct 25 '22 at 17:59
  • I am talking about _tracked_ files. – eftshift0 Oct 25 '22 at 18:00
  • Ok, let's say, I'm on branch A, and tracked-file A.txt. Then, edit A.txt, now how can i checkout to branch B. I run git diff --name-only HEAD..A, resultL A.txt – Manh Do Oct 25 '22 at 18:08
  • In that case _you can't_. Pick a file to modify among the files that are -->> **not** <<-- changed between `A` and `B`. Modify that file, then try the checkout to `B` and it will work. – eftshift0 Oct 25 '22 at 18:15
  • how to pick a file? I really don't understand this situation – Manh Do Oct 25 '22 at 18:24
  • ??? Just select a file that is *not* different between A and B. Say the project has 100 files. You run diff betwern A and B.... you get 2 files... modify one of the other 98 files that are **not** different between A and B. – eftshift0 Oct 25 '22 at 18:34
  • Thanks for you explanation, I really appreciate it. I edited the my answer. I don't know, should I delete my answer. Tks – Manh Do Oct 25 '22 at 19:05
  • I don't think you need to delete it..... unless you think it's plain **wrong**. I am very happy to help. – eftshift0 Oct 25 '22 at 19:08
0

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.

LeGEC
  • 46,477
  • 5
  • 57
  • 104