I tried 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 checkout to branch test
all local modifications from master
get merged.
Why?
I expected that git
refuses checkout to test
because of the local changes. I expected that git asks for a commit or stash
the local changes.
Edit: I used a bash script to execute this commands. I get 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'
Git merging combines sequences of commits into one unified history of commits. There are two main ways Git will merge: Fast Forward and Three way. Git can automatically merge commits unless there are changes that conflict in both commit sequences.
Checkout old commitsSince this has the potential to overwrite local changes, Git forces you to commit or stash any changes in the working directory that will be lost during the checkout operation. Unlike git reset , git checkout doesn't move any branches around.
In the merge box, click Disable auto-merge.
To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch.
git
tries hard to not lose possibly valuable data. In this case, it's not actually merging branches, as the changes in question have not been committed. Rather, when you do a git checkout
, it attempts to preserve newly made but not committed yet modifications, so it checks out the commit you are requesting, and adds your uncommitted changes. If you really want to discard the uncommitted changes, use git checkout -f
, or git checkout
followed by git reset --hard HEAD
. Sometimes, if the changes you have not committed yet can't be merged into what you're checking out cleanly, you'll get an error message and the checkout will fail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With