Quantcast
Channel: User Schwern - Stack Overflow
Viewing all articles
Browse latest Browse all 581

Answer by Schwern for How do we pull code into local main branch without having to commit the local changes afterwards?

$
0
0

You've already merged your branch into main, and the "169 changes that need to be pushed" indicates you've made many more changes to main. You can't simply update your main and then create a pull request, there's too many other changes all tangled up.

We'll need to untangle just your original PR commits into their own branch on top of the latest upstream main. I'll explain.

The simple linear history presented on Github and git log is very misleading. Your repository actually looks something like this:

      3 - 4 - 5 - 6 [origin/main]     / 1 - 2 ---------- M - A - B - C ... [main]     \         /      Z - Y - X

You can see something like this with git log --graph --decorate --oneline, but rotated 90 degrees.

Commits 1 - 6 were made by the original project. You cloned at commit 2 and the project committed 3 - 6 since. You made a branch and committed Z, Y, X and made a pull request. Then you merged that branch back into main and have since made additional commits on top of that. Now Z - Y - X are buried under additional commits.

What we want is this...

                    Z1 - Y1 - X1 [your-branch]                   /      3 - 4 - 5 - 6 [origin/main]     / 1 - 2 ---------- M - A - B - C ... [main]     \         /      Z - Y - X

We want to rewrite your PR commits Z - Y - X on top of the new tip of the project's main as a new branch. Then that branch can be pushed as a PR.


First, git fetch origin to make sure your copy of the upstream repository is up to date; this won't affect any of your local branches. Then make a branch from origin/main and switch to it.

$ git fetch origin$ git branch your-branch origin/main$ git switch your-branch      3 - 4 - 5 - 6 [origin/main][your-branch]     / 1 - 2 ---------- M - A - B - C ... [main]     \         /      Z - Y - X

Now copy your original commits with git cherry-pick. I've used Z - Y - X as placeholders. There's various ways you can get your original commit IDs. Simplest is to copy them from your original PR, even if it's closed.

$ git cherry-pick Z Y X                    Z1 - Y1 - X1 [your-branch]                   /      3 - 4 - 5 - 6 [origin/main]     / 1 - 2 ---------- M - A - B - C ... [main]     \         /      Z - Y - X

There may be conflicts, resolve them normally. Now you can push your-branch and submit it as a PR.


Viewing all articles
Browse latest Browse all 581

Trending Articles