Switch to another branch without changing the workspace files

前端 未结 8 1416
深忆病人
深忆病人 2021-01-30 06:26

I cloned a git repository from GitHub, made some changes and some commits; I made quite a lot and all are quite dirty, so they\'re not suitable for a pull request. Now I created

8条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-30 06:55

    Another way, if you want to create a new commit instead of performing a merge:

    git checkout cleanchanges
    git reset --hard master
    git reset cleanchanges
    
    git status
    git add .
    git commit
    

    The first (hard) reset will set your working tree to the same as the last commit in master.

    The second reset will put your HEAD back where it was, pointing to the tip of the cleanchanges branch, but without changing any files. So now you can add and commit them.


    Afterwards, if you want to remove the dirty commits you made from master (and assuming you have not already pushed them), you could:

    git checkout master
    git reset --hard origin/master
    

    This will discard all your new commits, returning your local master branch to the same commit as the one in the repository.

提交回复
热议问题