test1.txt git add test1...">

Git checkout automatically merges local modifications

I tried the following commands on the shell

git init echo "test1" > test1.txt git add test1.txt git commit -a -m "test1" echo "test2" >> test1.txt git branch test git checkout test 

text.txt now contains:

 test1 test2 

After checking in the test branch, all local changes from master are merged.

Why?

I was expecting git give up checking before test due to local changes. I was expecting git to request commit or stash local changes.

Edit: I used a bash script to execute these commands. I get the following output:

 r@r :/tmp/test$ ./createrepo Initialized empty Git repository in /tmp/test/.git/ [master (root-commit) 0407f5b] test1 1 file changed, 1 insertion(+) create mode 100644 test1.txt M test1.txt Switched to branch 'test' 
+6
source share
3 answers

git tries not to lose perhaps valuable data. In this case, it does not actually merge branches, since these changes were not fixed. Rather, when you do a git checkout , it tries to save recently made but not fixed changes, so it checks the command you request and adds your uncommitted changes. If you really want to undo uncommitted changes, use git checkout -f or git checkout and then git reset --hard HEAD . Sometimes, if the changes you have not made yet cannot be combined into what you are checking, you will receive an error message and the check will fail.

+14
source

Do you use any option with git checkout command? I ask because the behavior you describe is similar to when "git checkout -f" is used.

0
source

git checkout replaces locally modified files .

git merge combines local and incoming changes, warning of any conflicts.

Neither of these commands will delete local files that do not exist in the incoming commit.

Only git reset --hard completely destroy local changes.

0
source

Source: https://habr.com/ru/post/927365/


All Articles