旧版GitHub Desktop分支更新功能的Git命令行替代方案及原理咨询
Hey, I totally get missing that old GitHub Desktop button—it was super handy for quick merges with a dedicated commit! Let's break down exactly what it did and how to replicate it with Git commands, using your example scenario.
Core Git Command to Use
The key command is straightforward, but you need to make sure you're on the right branch first. The old button's core action is equivalent to:
# Make sure you're on your target branch (the one you want to update) git switch your-sub-branch # Merge the source branch (e.g., master) into it git merge master
Exact Logic of the Old GitHub Desktop Button
Behind the scenes, the app was doing these steps automatically for you:
- Validate branch state: Confirmed you had the target branch (your sub-branch) checked out.
- Sync source branch: If your local copy of the source branch (e.g., master) was out of sync with the remote, it would quietly pull the latest commits first.
- Execute merge: Ran
git merge <source-branch>. Since your sub-branch had new commits (D) that the source branch didn't, Git couldn't do a "fast-forward" merge—it had to create a new merge commit (G) that ties both branches' histories together. - Handle commit message: Auto-filled a default merge commit message (like "Merge branch 'master' into your-sub-branch") and saved it for you.
Note: If you were merging a source branch where a fast-forward was possible (no new commits on your sub-branch), the old button actually used git merge --no-ff <source-branch> to force a merge commit, instead of just moving the branch pointer. That's a small but important detail if you ever run into that scenario.
Step-by-Step Walkthrough of Your Example
Let's map this to your specific commit history:
- master: A → B → C → E → F
- sub-branch: A → B → C → D
Here's how to create merge commit G:
Switch to your sub-branch:
# Use checkout if you're on an older Git version git checkout sub-branch # Or switch (newer Git versions) git switch sub-branchEnsure master is up-to-date (skip this if all commits are local):
git switch master git pull git switch sub-branch # Alternatively, fetch without switching branches: # git fetch origin masterRun the merge command:
git merge masterGit will open your default text editor with a pre-filled commit message (you can edit it if you want). Save and close the editor, and Git will create the merge commit G.
Your sub-branch's history will now look like:A → B → C → D → G
Where G has two parent commits: D (the last commit on your sub-branch) and F (the last commit on master).
内容的提问来源于stack exchange,提问作者Ant

