search

Home  >  Q&A  >  body text

git 切换分支的时候 是否需要提交当前已经修改的

官方文档有句话“切换分支的时候最好保持一个清洁的工作区域。”,不知道我的理解是不是正确的。如果你当前分支有修改,但是还没有commit,如果你要切换分支的话,最好就是把内容都commit了?还是只用add到暂存区,下次回来可以继续。

阿神阿神2801 days ago808

reply all(5)I'll reply

  • 大家讲道理

    大家讲道理2017-04-21 11:18:00

    I recommend you use git stash to temporarily save it, and when you switch back, use git stash apply to reacquire the changes just now. Give you a clean working directory when switching:)

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-21 11:18:00

    There are several processing methods as follows:
    1. Add and commit, then checkout, and commit to the current branch
    2. Add but not commit, you can stash, then checkout and stash apply, then commit, commit to the current branch
    3. Add but do not commit or stash, directly checkout, and then commit, the record will be under the switch branch.

    The reason behind it: A local git repo has only one workspace and staging area, but has multiple branch submission areas, and our checkout just switches the HEAD pointer from one branch to another.

    reply
    0
  • 怪我咯

    怪我咯2017-04-21 11:18:00

    If your current branch has been modified but has not been committed yet, if you want to switch branches, is it best to commit all the contents?

    No, because your change is not necessarily ready for commit. Depending on your actual situation, you can use git stash to temporarily store it, you can commit to generate a new submission, or you can use git checkout -f to force branch switching

    reply
    0
  • 怪我咯

    怪我咯2017-04-21 11:18:00

    There is no commit and branch switching, the current file is still in the buffer.
    It is not committed to the current Git version.
    You can also reset, modify or commit under any branch.

    Test

    git config --global alias.co checkout
    git config --global alias.br branch
    git config --global alias.ci commit
    git config --global alias.st status
    
    git br a
    git br 
    git br b
    
    git co a
    touch a.py
    git add .
    
    git co b
    git st
    git add .
    git commit -m 'add a.py file'
    git st
    
    当前文件就回出现b分支下
    如果这样,退回上次commit
    git reset --hard

    reply
    0
  • PHP中文网

    PHP中文网2017-04-21 11:18:00

    If there is no one with the same name, you can switch whether it is in the staging area or not tracked. After the switch, everything is business as usual.
    But if there is one with the same name, git will remind you that it is easily overwritten and will not allow you to operate.

    reply
    0
  • Cancelreply