- 撤銷本地修改
[root@xuhx-02 git_project]# git status
# 位於分支 master
# 還沒有暫存以備提交的變動:
# (使用 "git add <file>..." 更新要提交的內容)
# (使用 "git checkout -- <file>..." 丟棄工做區的改動)
#
# 修改: README.md
#
修改還沒有加入提交(使用 "git add" 和/或 "git commit -a")
[root@xuhx-02 git_project]# git checkout -- README.md
[root@xuhx-02 git_project]# git status
# 位於分支 master
無文件要提交,乾淨的工做區
- 撤銷git add
[root@xuhx-02 git_project]# git add 01.sh
[root@xuhx-02 git_project]# git status
# 位於分支 master
# 要提交的變動:
# (使用 "git reset HEAD <file>..." 撤出暫存區)
#
# 新文件: 01.sh
#
[root@xuhx-02 git_project]# git reset HEAD 01.sh
[root@xuhx-02 git_project]# git status
# 位於分支 master
# 未跟蹤的文件:
# (使用 "git add <file>..." 以包含要提交的內容)
#
# 01.sh
提交爲空,可是存在還沒有跟蹤的文件(使用 "git add" 創建跟蹤)
- 撤銷commit
[root@xuhx-02 git_project]# git commit -m "add 01.sh"
[master 9d8cbe9] add 01.sh
1 file changed, 10 insertions(+)
create mode 100644 01.sh
- --mixed :不刪除工做空間改動代碼,撤銷commit,而且撤銷git add . 操做
[root@xuhx-02 git_project]# git reset --mixed HEAD^
[root@xuhx-02 git_project]# git status
# 位於分支 master
# 未跟蹤的文件:
# (使用 "git add <file>..." 以包含要提交的內容)
#
# 01.sh
提交爲空,可是存在還沒有跟蹤的文件(使用 "git add" 創建跟蹤)
- --soft :不刪除工做空間改動代碼,撤銷commit,不撤銷git add .
[root@xuhx-02 git_project]# git reset --soft HEAD^
[root@xuhx-02 git_project]# git status
# 位於分支 master
# 要提交的變動:
# (使用 "git reset HEAD <file>..." 撤出暫存區)
#
# 新文件: 01.sh
#
- --hard :刪除工做空間改動代碼,撤銷commit,撤銷git add .注意完成這個操做後,就恢復到了上一次的commit狀態。
[root@xuhx-02 git_project]# git reset --hard f51b0fdfa6846f88b90ac76e7975594bdd887576
HEAD 如今位於 f51b0fd add readme
[root@xuhx-02 git_project]# ls
README.md
- --amend:修改的文件已被git commit,但想再次修改再也不產生新的Commit
# 從新提交
$ git add sample.txt
$ git commit --amend -m"說明"
- 清除工做空間 git clean
[root@xuhx-02 git_project]# git clean -n
將刪除 01.sh
- 參考git 撤銷回滾學習