6

I'm having a problem with editing commits.

I have 2 commits for 2 .php files. My goal is to edit them both. I read about rebase -i and this is what I reckon is supposed to be done:

  • first I change pick to edit;
  • save and exit;
  • rebase stops;
  • type git commit --amend;
  • make changes, save and close the text editor;
  • type git rebase --continue.

After this I believe the rebase stops again and I have to do this all over again for the second commit.

But right after I type git rebase --continue i get this:

file1.php: needs update You must edit all merge conflicts and then mark them as resolved using git add 

What is the problem and what do I do?

4
  • Yes, those are the only two commits in the entire history. As for the second question, when I open the file it says 'The file changed on disk. Do you want to reload the file'. Is it supposed to do that? Commented Jan 19, 2014 at 1:02
  • Is it an option to squash the two commits into one and edit that one? Commented Jan 19, 2014 at 1:59
  • No. I have to change both files. Commented Jan 19, 2014 at 2:21
  • Please don't add solutions inside questions - post them as an answer. Commented May 2, 2014 at 10:06

3 Answers 3

10

When you stop for the rebase you have to:

  • edit a file(s)
  • add changes to the index - git add changedFile
  • amend changes - git commit --amend
  • continue rebasing - git rebase --continue

From your description it is possible that you forgot to add changes to the index. In that case git commit --amend does nothing (there is no changes to amend). In addition you have git commit --amend before you edit the file which is also wrong (you have to amend changes you already did on the file).

Try to apply steps in the order I gave.

Sign up to request clarification or add additional context in comments.

6 Comments

I gave it a try. I changed the file, did the 'git add', did the '--amend' and the '--continue'. Then i got this error: could not apply 21e61c9... second commit. Git then refers me to --continue, --skip and --abort commands.
That error is expected when your change conflicts with the original changes in the following commits. Call git status to see how does it look like. Pay attention to the line with both modified.
# HEAD detached from 7deb658 # You are currently rebasing branch 'master' on '7deb658'. # (fix conflicts and then run "git rebase --continue") # (use "git rebase --skip" to skip this patch) # (use "git rebase --abort" to check out the original branch) # # Unmerged paths: # (use "git reset HEAD <file>..." to unstage) # (use "git add <file>..." to mark resolution) # # both modified: show-dir-content.php
As predicted you have a conflict in show-dir-content.php. Did you make a change in this file which can conflict/overlap with the changes in the following commits? Try to resolve it.
Yes, i made changes in the file, my task IS to make changes to both files I previously committed. I'll look for a solution in the link you provided.
|
1

(Posted solution on behalf of OP).

I got the right way to do it:

git rebase -i --root (I wasn't able to find both commits using HEAD~2) pick->edit for both commits Ctrl+X and Y and ENTER edit the first commit git add file1.php git commit --amend git rebase --continue edit the second commit git add file2.php git commit --amend git rebase --continue git push -f 

Hope this helps at least one person starting their git experience. Szpak, you've been a big help. Thanks.

1 Comment

Use git rebase -i origin instead of --root; otherwise, you'll rebase existing commits, which can be very problematic
0

If you are on a feature branch on which you are working alone:

First make the changes to the file and create a new commit.

git commit -m "message here" 

Make sure you are on your feature branch

git checkout your_feature_branch 
git rebase -i parent_branch (the branch the your_feature_branch was forked from) 

The editor will the pop up with options. Change the tag from squash for the 2nd line. You will then have the option to edit your commit message. You can remove the one commit message and update with different text.

Shift+zz (exit for vi) 

If you attempt git push your_feature_branch here you will receive the following error:

$ git push your_feature_branch To https://your_repo.git ! [rejected] your_feature_branch -> your_feature_branch (non-fast-forward) error: failed to push some refs to 'https://your_repo.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. 

Which is expected because of the local changes you made - to overcome this use:

git push your_feature_branch --force 

The above command will push the changes to remote repo.

NOTE: Usage of --force is not standard or recommended so only use this if you know what you are doing. It is permitted in this case because the assumption is that you are the only one working on your_feature_branch

I am assuming you are working alone on your feature branch.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.