1. initialization:
git init
2. modify and add a file
git add file
git commit -m "message"
3. compare difference
git diff
git status
If you know that the remote version is what you want then you can fix it by doing the following:
git fetch origin
git reset --hard origin
Now you can git push and pull to your heart’s content.
4. restore a commited file
git log # to find the file tags
git reset --hard HEAD^
git reset --hard #filetag
5. checkout a saved file to relplace the current file in workplace
git checkout -- filename
6. delete
git rm filename
git commit -m "remove"
From:
场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file
。
场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD <file>
,就回到了场景1,第二步按场景1操作。
场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考版本回退一节,不过前提是没有推送到远程库。