Yves
Yves

Reputation: 12371

How to explain the behavior of git reset --soft && git checkout

I've been using git for one year and I'm able to use the common git commands, such as git add, git commit, git reset, git checkout, git rebase etc.

Today, I found something about git checkout that I can't explain.

As all we know, git checkout <branchname> can switch to the <branchname>. As my understanding, this command do the following things:

In a word, this command does the three steps so that it can switch the branch.

And as all we know, git reset --soft can reset the HEAD pointer.

Now let me show you something "weird" to me.

git checkout br1
git reset --soft br2
git checkout br2

As my understanding about git checkout above, I thought that git reset --soft br2 was nonsense, which means that as my own opinion, the three commands are equivalent to git checkout br1 && git checkout br2.

However, after executing these three commands, I find that the last commit of br1 is brought to the branch br2, which means that the last commit of br1 has been copied to the current working zone and the current index.

Well, I'm confused now. I suddenly feel that the commands git checkout and git reset are kind of strange, I think I might have misunderstood the command git checkout for a long time...

Upvotes: 2

Views: 182

Answers (1)

Schwern
Schwern

Reputation: 164669

And as all we know, git reset --soft can reset the HEAD pointer.

True, but that's incidental. git reset lets you arbitrarily move the current branch to a new commit, and then checks that commit out. --soft vs --hard just say what to do about the working directory and index.

git checkout br1
git reset --soft br2

This moves br1 to the same commit as br2 while keeping your working directory and index the same as br1.

Upvotes: 2

Related Questions