Before getting ahead lets clear these three fundamental things in git.
Git is version control system, or in simple defination(bit vaguely) it helps to create point in time snapshots of current directory.
HEAD: It is a symbolic reference pointing to wherever you are in your commit history.
Index: The index, also known as the staging area, is the set of files that will become the next commit. It is also the commit that will become HEAD’s parent.
Working Copy: This is the term for the current set of files you’re working on in your file system.
Few more simple concept:
Note:
Git branches are just an alias to git sha-commit. In git all things are tracked by sha2 hash. Hashes are long and tedius to use,hence we use some name/pointer and updated those name to new hashes to move ahead.
So normal workflow is like:
But suddenly we came to know while commiting that we have one extra file which we added in index is not required to push in git repository. It means we don’t want that file in index. Now the question is how to remove that file from git index, Since we used git add to put them in the index it would be logical to use git rm? Wrong! git rm will simply delete the file and add the deletion to the index. So what to do now:
Use : git reset
git-reset - Reset current HEAD to the specified state
Default option clears INDEX, leaves your working directory untouched. (simply unstaging everything).
It can be used with number of options with it. Three main options to use with git reset:
–mixed
git reset --soft <ref>
:
<ref>
(sha ID).<ref>
, it takes as HEAD.git reset --soft
or git reset --soft HEAD
: It does nothing, because is checks out HEAD to HEAD again.git reset --soft @~1
: It would reset the HEAD of current pointed branch to parent of current branch, it won’t touch staging/index area or working directory.git reset --mixed <ref>
or git reset <ref>
:
<ref>
is given, it just resets index as last commit(i.e. HEAD).
git reset --hard
or git reset --hard <ref>
:
--mixed
does but also cleans the working copy.<ref>
if specified or else to last commit.Reference: