yingele
yingele

Reputation: 21

How does git reset <filename> work?

There are 2 statements about git reset that are true but I find them contradictory given my limited knowledge of how Git works.

Is there a way to reconcile the 2 statements?

Statement 1: Git reset file1 will un-add file1 from the staging area.

This is the command to use when you added file1 to staging and you changed your mind and want to remove it from the staging area.

Statement 2: Git reset file1 will copy files from HEAD to the staging area.

According to statement 2, git reset adds files to the staging area.

So how do the files become removed from the staging area when they were just copied to it from HEAD?

Upvotes: 0

Views: 1769

Answers (2)

Vrushali Raut
Vrushali Raut

Reputation: 1188

How does git reset work?

Lets take an example :-

We added octodog.txt into our family octofamily

git add octofamily/octodog.txtcode

So now that octodog is part of the family, octocat (our previous file) is all depressed.

Since we love octocat more than octodog, we'll turn his frown around by removing octodog.txt

You can unstage files by using the git reset command. Go ahead and remove octofamily/octodog.txt.

git reset octofamily/octodog.txt

git reset did a great job of unstaging octodog.txt, but you'll notice that he's still there. He's just not staged anymore. It would be great if we could go back to how things were before octodog came around and ruined the party.

Files can be changed back to how they were at the last commit by using the command:

git checkout -- <target>

Go ahead and get rid of all the changes since the last commit for octocat.txt

git checkout -- octocat.txt

for further reference and git knowledge here is one git game

hope it will clear your all git related doubt's :)

Upvotes: 0

mmlr
mmlr

Reputation: 2165

HEAD refers to the head of the current branch, i.e. the newest commit. When you change a file, the file in your working copy differs from HEAD. Staging that difference means copying the file from the working copy into the staging area.

Unstaging means removing a difference against the HEAD from the staging area. Hence unstaging is done by copying the state from HEAD back into staging area.

Upvotes: 2

Related Questions