git 常用操作汇总

git 仓库特点

  1. 使用快照流
  2. 近乎所有操作都是本地执行
  3. 一般只添加数据
    三种状态:modified -> committed -> staged
在 Git 中任何已提交的东西几乎总是可以恢复的。甚至那些被删除的分支中的提交或使用 –amend 选项覆盖的提交也可以恢复。然而,任何你未提交的东西丢失后很可能再也找不到了。

config

全局读:~/.gitconfig(在无.git目录下 或者 加上–global 参数)
项目读:.git/config

  1. git config –list
    列出当前所有配置信息
  2. git config <key>
    查询某一项配置信息

status

git status -s 简短输出各文件状态

diff

git diff 查看未放入暂存区的文件变化
git diff –cached 查看已放入暂存区的文件变化

commit

git commit -a 可以跳过 git add . 步骤
git commit –amend 提交后发现忘记了暂存某些需要的修改,可以使用此命令,最终只会有一个提交 – 第二次提交将代替第一次提交的结果

reset

  1. git reset:回滚 git add 操作
  2. git reset –soft HEAD^:回滚最近一次提交(commit)
  3. git reset –hard HEAD~n:永久删除最近的 n 个提交
  4. git reset –hard:回滚 git pull 操作
  5. git reset — {fileName}:回滚 git add fileName 操作
  6. git reset –keep {tagName}:回滚到 tagName 之前

rm

git rm 从已跟踪文件清单中移除(确切地说,是从暂存区域移除)
git rm –cached 把文件从 Git 仓库中删除(亦即从暂存区域移除),但仍然希望保留在当前工作目录中

mv

git 不会显示地跟踪文件的重命名和移动
所以需要使用git mv命令
git mv {originFile} {targetFile}
相当于以下三条命令
mv {originFile} {targetFile}
git rm {originFile}
git add {targetFile}

log

git log 查看提交历史
-p 查看提交历史的变化
-2 查看最近两次提交
–stat 查看简略信息
–pretty=oneline 一行表示一次提交信息
–pretty=format:”%h – %an, %ar : %s” 指定格式显示提交信息

tag

  1. git tag:列出已有的标签
  2. git tag -a {tagName} -m {‘description’}:创建标签
  3. git tag -s {tagName} -m {‘description’}:使用私钥创建标签
  4. git tag -d {tagName}:删除标签
  5. git tag {tagName}:创建轻量级标签(无描述)
  6. git tag -a {tagName} {HEAD} -m {‘description’}:对某一次提交打上标签
  7. git push –tags:分享标签

show

git show 命令指定提交ID(hash HEAD)来查看具体的变化

stash

git stash 将当前工作区的更改隐藏起来 保存在一个栈中
git stash pop 将隐藏栈顶部的工作去更改推出
git stash list 显示当前的隐藏栈

git工作流程

  1. 将Git的一个存储库克隆为工作副本。
  2. 可以通过添加/编辑文件修改工作副本。
  3. 如有必要,还可以通过让其他开发人员一起来更改/更新工作副本。
  4. 在提交之前查看更改。
  5. 提交更改:如果一切正常,那么将您的更改推送到存储库。
  6. 提交后,如果意识到某些错误并修改错误后,则将最后一个正确的修改提交并将推送到存储库。

《git 常用操作汇总》 image.png

rebase

好用的 git rebase

shortlog

git shortlog 统计提交(commit)次数
-s 只显示次数,不显示commit描述
-n 从多到少排序

.gitignore 存放忽略文件

# no .a files
*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in the build/ directory
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory
doc/**/*.pdf
    原文作者:大雄的学习人生
    原文地址: https://www.jianshu.com/p/1df104d9594d
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞