简体   繁体   中英

git mv a bash script to rename a folder of a specific branch inside changes all branches

I'm not quite sure if this is a macOS/zsh specific problem so I'm going to add the tags anyway. Consider this dummy repository , with some dummy folder and files:

test_20200324:master
   |-> test_folder_one
   |     |->....
   |-> test_folder_two
   |     |->....
   |-> test_files...
   |-> setup.sh

and a master branch. The setup.sh is instructed to create a new branch, checkout and rename a folder:

git branch production
git checkout production 

git mv test_folder_two test_folder_four

now after running the script, I expect to see a new branch:

test_20200324:production
   |-> test_folder_one
   |     |->....
   |-> test_folder_four
   |     |->....
   |-> test_files...
   |-> setup.sh

while the content of the master branch is intact. However, for some reason the script messes the whole repository up (lucky I haven't run that on real production!). The script has renamed that specific folder in all branches. And master branch also has its test_folder_two folder renamed to test_folder_four . I would appreciate it if you could help me understand what is the problem and how I can resolve it.

When you run git mv , that change is part of your working tree state (and also staged in the sense described in What does "stage" mean in git? ), but not yet part of your branch.

This means that if you changed branches, and git didn't then try to retain working-tree changes (like that mv ), then your changes would simply be thrown away.

Throwing away someone's work is bad -- it's not something a source control system should do. Thus, when you switch from one branch to another, git tries to apply uncommitted working tree changes to that new branch (and if they conflict / can't be applied, refuses to complete the checkout).


So, how can you stop that from happening?

Just run git commit -m 'Renamed folder two to four' , and the change will be firmly put in place on the branch you already have checked out, instead of following your working tree.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM