Creation a repository
CD path\directory to enter the directory to be used as the repository
After a few files has been created in the repository directory, we need to add the changes to this repository
1. Add changes to the stage(index)
git add Readme.md
git add BaseUsage.md
2. Committing changes to the repostiory. The option
-m is very important. So we konw what we changed
git commit -m "Creation the readme.md file and the baseusage.md"
View the status of repository
We must know status of repository before we can continue our work. If we have changed some files, we’d better know what we changed in the files
Now, we konw that the changes are correct. we can add the file to the stage and commit to repository.
git add BaseUsage.md
git commit -m "Add some contents in BaseUsage.md . View the staus of repository"
View commit log and roll-back
Before I write this line I committed a wrong version of this file the version named “Wrong information”,then rolled it back. What did I do?
First, we have to know how many commits are in this repository.
git log --oneline
We can see the last commit on the top, and be tagged “HEAD -> master”. The older commits are in below.
The tag “HEAD” means this commit is the current version.
Now, let’s roll-back commit.
git reset --hard HEAD^
This command will roll-back one version commit. If you want to roll-back two version or more, you can use this:
git reset --hard HEAD^^
git reset --hard HEAD^^^^
(Let’s ignore the option “–hard” for now)
If you check the status of this repository. You can see that the last previous commit was discarded.
Recall the roll-back
We rolled back the commit earlier, but we don’t want to do that now. So what should we do?
First we should know the serial number of commit that was discarded before.
This command can print the changing log.
Now we can find the serial number of the commit that we discarded before. Then recall the roll-back.
git reset --hard "serial number"
About Working tree
We have an branch “Master”(repository),our changes were commited to “Master”. When we edit or create files, the files all save in working tree, which is the folder we see on the computer.
After we change files in this folder, we can see the diffrence between working tree and “master”. If we affirm the changes, we can add the changes to stage.
Does add the changes to Stage can make repository be latest. No. Stage and repository are different place.
There are three place: Working Tree, Stage and repository.
We change files in working tree . Then add changes from working tree to to Stage . Last, We commit changes from stage to repository.
git diff is to compare difference between working tree and stage. when we add changes from working tree to stage , the difference disappear
How do we konw the difference between working tree and repository? This command can tell us.
git diff HEAD
git diff HEAD -- "file"
We can undo changes manual in working tree
Or use this command
git checkout -- "file" to discard chages in working tree.
If the changes are added to stage we also can unstage
git reset HEAD "file"
This command meaning that the changes roll-back from stage to working tree. Then we can undo changes in working tree.
About how to undo changes in repository? Do you remember roll-back commit?
But remember that roll-back commit will change working tree.
That will make a difference if we delete a file from file explorer. So what shall we do?
If the change is correct, we have to remove the file in repository and commit.
git rm "file"
git commit -m "Remove "file" "
If we would like to restore file that was deleted in working tree before, what shall we do?
Remember undo command
git checkout "file"?
This command will restore changes in working tree from repository.