Coder Social home page Coder Social logo

Git 常用命令 about blog HOT 1 OPEN

buddywang avatar buddywang commented on July 24, 2024
Git 常用命令

from blog.

Comments (1)

buddywang avatar buddywang commented on July 24, 2024

解密 reset,顺便比较 checkout

概要

git 的思维框架可以看做管理三棵树(树?三个东西也行):HEAD(一个指针,指向该分支最后的一次提交)、Index(暂存区)、Working Directory(工作区)
image
reset 命令通常是下面的样子

# 没有 [commit](hashID) 则表示最新一次提交
git reset [--soft/--hard] [commit]

它可以看作主要做了这三步:

    1. 移动 HEAD 指向 [commit],若加了 --soft,则就此停止
    1. 将 HEAD 对应的内容复制到 Index,若没有 --hard,则就此停止
    1. 将 Index 的内容复制到 Working Directory

应用

reset 命令可以有以下应用(根据上面步骤看):

  • 取消暂存
# 若没有 [path] 则表示取消所有暂存
git reset [path]
  • 撤销修改 或 回滚到某一次提交
# 若没有 [commit] 则表示撤销修改,有则表示回滚到该提交
git reset --hard [commit]
  • 修改最后一次的提交(重新提交)
# HEAD~ 表示 HEAD 的父提交(上一次提交)
git reset --soft HEAD~; git commit -m 'xxx'

总结

下面的速查表列出了命令对树的影响。 “HEAD” 一列中的 “REF” 表示该命令移动了 HEAD 指向的分支引用,而 “HEAD” 则表示只移动了 HEAD 自身。 特别注意 WD Safe? 一列——如果它标记为 NO,那么运行该命令之前请考虑一下。

-- HEAD Index Workdir WD Safe?
Commit Level        
reset --soft [commit] REF NO NO YES
reset [commit] REF YES NO YES
reset --hard [commit] REF YES YES NO
checkout HEAD YES YES YES
File Level        
reset [commit] NO YES NO YES
checkout [commit] NO YES YES NO

参考

https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E9%87%8D%E7%BD%AE%E6%8F%AD%E5%AF%86

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.