Search code examples
gitgithubgit-rebasegit-squash

How can i do git rebase after git pull request?


I have a trouble git rebase. because, I had pushed my local code to origin dev. And, I send a pull request to 'upstream'. But, There are 1 PR and 7 commits like below the picture enter image description here

I want to 7commits squash to 1 commit.

What shall I do?

( I have tried : 'git rebase -i HEAD~7', but I have met the message that 'error: cannot rebase: You have unstaged changes.' )


Solution

  • As per my comment, you must commit, stash, or discard all your changes before rebasing. If your changes are complete, you most likely want to commit them (git add --all followed by git commit).

    If they are not complete but you want to continue working on them later, use git stash to save your unfinished changes while also resetting to HEAD. Perform your rebase and then recover these changes with git stash pop.

    To instead discard all your changes, git reset --hard HEAD will do the trick; reset to unstage your changes, --hard to discard them, and HEAD to specify the commit to which you are resetting. You should then be able to rebase.

    It's worth noting that a stash or reset will not affect untracked files. To have these files included, you must first stage them with git add.