You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Git操作求助:将master分支本地未提交变更迁移至其他分支

Hey there! I’ve been in this exact spot before—accidentally cramming a week’s work into master and panicking about moving it over. Don’t worry, we can fix this without losing a single line of code. Here’s your step-by-step playbook:

Option 1: Directly move changes to a new branch (quickest method)

This works if you have unstaged or staged uncommitted changes in your local master branch:

  1. Create and switch to your new non-master branch
    Pick a descriptive name (like feature/weekly-dev-work or bugfix/user-profile-update) and run:

    git checkout -b feature/weekly-dev-work
    # Or using Git 2.23+, you can use the more intuitive:
    git switch -c feature/weekly-dev-work
    

    Your uncommitted changes will automatically carry over to this new branch—Git keeps work in progress in your working directory, not tied to a specific branch yet.

  2. Verify your changes are on the new branch
    Run git status to confirm: you’ll see the same uncommitted changes listed, but now they’re associated with your new branch instead of master.

  3. Commit your work properly
    Stage your files and commit as usual:

    # Stage all changed files (or specify individual files if needed)
    git add .
    # Write a clear commit message summarizing your week's work
    git commit -m "Implement user dashboard UI + API integration (week 1 work)"
    
  4. Reset master to its clean state (optional but recommended)
    If you want to make sure your local master branch is back to how it was before you started working, switch back to master and reset it:

    git checkout master
    # This will discard any uncommitted changes on master (safe since we moved them!)
    git reset --hard HEAD
    

Option 2: Use git stash for extra safety (great if you’re nervous)

If you want to avoid any accidental missteps, stash your changes first to tuck them away temporarily:

  1. Stash your uncommitted changes
    This saves your work in a "stash" that you can retrieve later:

    git stash
    

    You’ll see a message like Saved working directory and index state...—your master branch is now clean.

  2. Create and switch to your new branch

    git checkout -b feature/weekly-dev-work
    
  3. Retrieve your stashed changes
    Pop the stash back into your new branch’s working directory:

    git stash pop
    

    Your week’s work will be right where you left it, now on the new branch.

  4. Commit and clean up master (same as Option 1)
    Follow steps 3 and 4 from Option 1 to commit your changes and reset master if needed.


Quick Notes to Remember:

  • Never run git reset --hard unless you’re 100% sure you’ve moved your changes elsewhere—it permanently discards uncommitted work.
  • If you’ve already pushed some of these changes to the remote master branch, that’s a different scenario (let me know if that’s the case and I’ll adjust the steps!).

内容的提问来源于stack exchange,提问作者Raj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:07:56