Background:
Versioning is critical for developers who have been using SVN (primarily TORTOISESVN) as a collaboration tool for teamwork. But the relevant open source project uses Git on GitHub. Due to the limitations of the project itself, very few people have modified common files and abandoned working directory local uncommitted changes. So there's a bit of ambiguity about Git's use. This blog post uses an example to explain how git discards uncommitted local modifications in working direcotry .
Git's repository structure:
Git has only one central repository compared to SVN, and each local part is a separate repository, often eventually merged into the same warehouse as the central repository by agreement. It is because of the distribution of git that git has a local repository and a remote repository, roughly the following:
The local repository and the remote repository are synchronized via pull and push, and the GIT local repository operation is more complex than SVN because there is a buffer index (also known as a staged) for git operations.
git checkout tutorial 1. svn revert discard local modifications and version rollback
Checkout in SVN is simply the first checkout of the code from the central warehouse to establish a local copy, after which a few simple operations such as update, commit, revert and so on to achieve the majority of version management work.
-"SVN update": Update can download the latest code from the SVN central repository to a local copy, here are a few things. First, there is no local modification, the local copy will be synchronized directly to the latest state of the server, and the local folder will display a green check mark (TortoiseSVN), and the second, locally modified but not with the latest revision of the central repository of the intersection, Updates will still be updated to the local copy, and a green checkmark (TortoiseSVN) will be displayed in the local folder, and the third, locally modified but with the latest modification of the central repository, will update the latest state to the local copy after update. However, a yellow exclamation mark (TortoiseSVN) appears in the local folder, indicating that there is a conflict that needs to be resolved manually.
-"svn revert": Revert the simplest is to discard local modifications, which is most commonly used. Another scenario is to roll back to the previous commit.
-"SVN commit": Commit is the local copy submitted to the central server.
The above is the approximate operation of SVN, its rollback operation is simple and clear.
2. Git discard working directory local modifications
As shown, Git is more complicated than SVN because of the staged buffer and the remote repository between the local and the repository (the reason git is suitable for distributed collaborative development is that each local repository can run independently from the remote repository, and both are in exactly the same position), So when git rolls back and modifies commits more complex than SVN, based on the above git reset and git checkout comparison, we can conclude that the staged is stored in a snapshot image, The image is the target of commit commit (of course, you can skip the stage buffer directly using COMMIT-A, and submit the working local working directory directly to the local repository). So how do you discard local changes in Git, and how do I roll back to a previous commit?
Here's an example of how to discard local working directory modifications in Git.
(1) Create a git repository
(2) submit three times to prepare the test environment
(3) Modify the A.txt test file to make working directory change
(4) git checkout discard changes to working directory
You can discard working directory modifications by following the instructions below:
git checkout .//或者git checkout a.txt
The specific results are as follows:
"Note 1": checkout Master or git checkout head via git cannot discard working directory modifications because the current location is the head of the master branch. The actual results are as follows:
"NOTE 2": since "Note 1" is because the current branch is already the HEAD of master and cannot discard working directory changes, then the git checkout head~n check out the different version can discard working What about the changes to the directory?
When you try it, the following error pops up:
Error:your local changes to the following files would is overwritten by checkout:
A.txt
Please commit your changes or stash them before you can switch branches.
Aborting
Because if you adjust to non-head commits at this point, it will conflict with local modifications. So if you want to roll back and view the previous version, the correct steps are:
-a --oneline //查看希望返回的之前版本号git checkout commit-number//commit-number是你想返回的版本git status //查看当前的状态
The actual results are as follows:
3. Git checkout version rollback git reflog
At this point we can see the status of the previous commit, but there is a problem. Enter Git log–oneline the log for multiple commits is missing. Didn't the previous version exist?
In fact, there is a way to see all the previous records, enter the following command:
git reflog
The results are as follows:
Note:
As mentioned earlier: Git is more complicated than SVN because of the staged buffer and the remote repository between the local and the repository (the reason git is suitable for distributed collaborative development is that each local repository can run independently from the remote repository, and both are in exactly the same position), So when git rolls back and modifies commits more complex than SVN, this introduction of discard local modifications is only for the Git repository trace file (tracked files) analysis, if you add a file not added to the repository (i.e. untracked files) should be discarded in conjunction with other GIT directives, which will be further explained later.
[Email protected]
Date: 2016-06-19
Project versioning: How git abandons local working directory modifications and rolls back