简体   繁体   中英

Changes gone after doing git rebase

I had made three commits in git. Like this.

asaurab-3.desktop% git lg
* 931b405 - (HEAD -> saurabh, origin/saurabh) minor fixes (6 minutes ago) <Saurabh Kumar Agarwal>
* 838d366 - adding upload and download functionality (15 minutes ago) <Saurabh Kumar Agarwal>
* 0d62348 - first commit (7 days ago) <Saurabh Kumar Agarwal>
* 4797854 - (origin/mainline, origin/HEAD, mainline) commit  (2 weeks ago) <Perforce Administrator>

I wanted to squash these three commits so i did

git rebase -i HEAD~3 

It opened a window, with three commits. I replaces all three pick to squash and exist from editor using :wq! . :wq! .

I got the error message

Cannot 'squash' without a previous commit

Now my git lg shows

* 4797854 - (HEAD, origin/mainline, origin/HEAD, mainline) commit (2 weeks ago) <Perforce Administrator>

All my changes are gone. How Can I get them back?

This is the output of git status

 git status
interactive rebase in progress; onto 4797854
No commands done.
Next commands to do (3 remaining commands):
   squash 0d62348 first commit
   squash 838d366 adding upload and download functionality
  (use "git rebase --edit-todo" to view and edit)
You are currently editing a commit while rebasing branch 'saurabh' on '4797854'.
  (use "git commit --amend" to amend the current commit)
  (use "git rebase --continue" once you are satisfied with your changes)

Your rebase has not completed yet. It's not an error, just a regular info while squashing. Run rebase --continue to finish the rebase.

$ git rebase --continue     # repeat the command until rebase complete

All my changes are gone. How Can I get them back?

OR , you can abort the rebase and checkout to your desired commit. You can choose from git reflog output.

$ git rebase --abort       # abort/stop the rebase

$ git reflog               # copy your desired commit hash
$ git checkout <commit>    # I guess here, desired commit = 931b405  

Now create a new branch

$ git checkout -b new-branch

You can squash last 3 commits by soft reset also.

$ git reset --soft HEAD~3
$ git add .
$ git commit -m 'Squash last three commits'

$ git push origin HEAD     # push to remote

Answer to the question is in git status only.

I did

git rebase --edit-todo

Then Earlier, I did squash in all three so this error was coming. I did pick for first commit and then squash for other two commit. then I saved my changes.

Then I did

git rebase --continue 

It solved my problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM