(Git_3) .gitignore, Blame, Checkout, Reset

.gitignore

If there is a file that we do not want to be tracked in git, create a file called .gitignore and type in the file names in .gitignore as follow:

Then after creating this file, the git still cannot track this file

As you can see, after creating secrect.html, there is still such file after we called the status command

Notice! If the file we want to ignore is already in the directory, even though we add the file name into .gitignore, this file cannot be ignore by git. The only way is to use — cached to remove the file from git. Then, it won’t be tracked by git; for example, after we change the content of test1.html

Git still detect the changes in test1.html, so we use — cached

and commit this move

Then git won’t track it anymore

delete all the file that are untracked by git

Blame

If we want to know who edited xxx.html file, we can type in following

Ckeckout

For example, if the files are accidentally deleted,

As you can see, all the html files are deleted

Then the git status:

git still knows that the files are delete, then we can use

to recover the files

Reset

For example, if we want to change the following commits for one step back

or

^ means go back to one step

If we want to go back to two steps, we can use ^^

or if we want to go back 100 steps, we can use ~ as follow:

There are three modes in reset, mixed, soft, and hard. The visualization:

The process of git: a file modified -> git add -> go into stage/index -> git commit -> go into history

Reset — Soft

It only moves HEAD to where we want and the files are still in stage, no need to git addagain but still need to git commitagain to go back to the current status

Hard

It moves the current status back to the file before modified. The whole commit will be delete

Mixed

It moves the status back to the a file modified. We need to git commitagain to go back before reset

The difference between checkout and reset — mixed

Both checkout and reset can recover commits. However, checkout does not change the current commit. It just ‘checkout’ the historical commit. But reset truly go back to the historical commit.

How to rescue files after git reset — hard

for the following commits,

Then we reset it

The latest commit is truly disappear

To recover the latest commit, given we remember the last index

Then the commits will go back

If we forget the index, we can use reflog

to get the historic index

reference: