git various stages of code revocation Profile

git various stages of code changes rollback undo

Code workspace revoked

[Step 1] Use git status to view the current status, you will be prompted to modify a file, as shown in me modify the main.cpp file workspace, you can see the prompt git given by two, one is to use git add area code works submitted to the staging area, there is a git checkout to fall back to the previous code is modified

Here Insert Picture Description

[Step 2] Enter the command git checkout --filename directly on a modified file rollback, git Checkout -. The current project file all changed files are revoked, I am here to do is to withdraw the main.cpp open the file found written is revoked, it can directly use cat to see if main.cpp file was revoked on git bash, when attention using the command change is to use the local repository file main.cpp main work area .cpp file replacement, thus withdrawn back to the original maic.cpp files , you can view the file contents cat, verify

Here Insert Picture Description


Staging area code for revocation

[Setp 1] to use git add to submit files to the staging area, and found the wrong code changes, you need to withdraw

Here Insert Picture Description
[Step 2] can use git reset HEAD or git reset HEAD filename , the first one is the staging area for all of the code's complete withdrawal, and the second is given back to the file filename, revoke successful, this step is actually code staging area to fall back to the work area, you also modify the code
Here Insert Picture Description


Local repository code revoked

[Step 1] careless use git commit -m "xxx" will modify the wrong code submitted to the local warehouse

Here Insert Picture Description
【step 2】 输入 git log 查看最近修改给的日志,会有每次修改的commit_id,下图中可以看出本地仓库分支比远程仓库分支对一次提交

Here Insert Picture Description
【step 3】 输入 git reset --hard commit_id 让HEAD指针指向你指定的 commit_id,实际就相当于回退,这次操作并没有删除任何东西,只是修改指针的指向,可以看到图中显示,将HEAD指着指向修改了

Here Insert Picture Description


如果你执行了上面的第三步,又想将原来的代码在回退回来 ,使用上面的例子就是想将修改的代码留下,即将HEAD的指针该回去,就可以使用 git reflog 查看对应的commit_id然后在使用 git reset --haed commit_id 将HEAD指针在修改回去即可,在图中可以看到HEAD指针的指向又被修改回去了

Here Insert Picture Description


远程仓库代码的撤销

【step 1】使用本地仓库代码撤销方式,先将HEAD指针的指向修改,使得本地仓库回退

Here Insert Picture Description
[Setp 2] input git push -f origin master forced to update the remote, because at this time commit your local repository is one step behind than commit a remote repository, so the need to add -f to force push the time of submission, should be noted that if you at the same time more than one person working on this project, you need to be careful when forced to push, best to use git log look at the change log, to prevent the forced push, overwrite someone else to write the code
Here Insert Picture Description
if we commit code command can not familiar with git see my previous blog frequently used commands git git commonly used commands

Released nine original articles · won praise 3 · Views 175

Guess you like

Origin blog.csdn.net/weixin_43604792/article/details/104038549