我的Git手册(二)

参考

Git Pro Book - Git Basic

获取Git仓库

有两种方式获取Git项目仓库:

  • 将尚未进行版本控制的本地目录转换为Git仓库
$ cd /home/user/my_project 
$ git init
  • 从其他服务器clone一个已存在的Git仓库
$ git clone https://github.com/libgit2/libgit2

#自定义本地仓库名称
$ git clone https://github.com/libgit2/libgit2 mylibgit

#支持其他协议如git:// ssh传输协议

更新记录到仓库

工作目录下每个文件只有两种状态:tracked(已跟踪)和untracked(未跟踪)。已跟踪的文件指加入了版本控制的文件,在上一次快照中有它们的记录,在工作了一段时间后,它们的状态可能是unmodified(未修改)、modified(已修改)或staged(已放入暂存区)

一些常用命令:

  • git status 查看当前文件状态
    • git status -s 状态简单预览
  • git add 开始跟踪一个新文件/添加到暂存区
  • git diff  比较当前工作目录当前文件和暂存区域快照的差异
    • git diff --staged|--cached 比较已暂存的和最后一次提交的文件差异
  • git commit 提交文件到本地仓库
    • git commit -m ""
    • git commit -a 跳过使用暂存区
  • git rm 从已跟踪文件清单中移除文件并删除
    • git rm --cached 移除但不删除
    • git rm -f
  • git mv 移动文件/重命名

查看提交历史

  • git log
    • git log -p|--patch -2 显示每次提交所引入的差异,最近两次提交
    • git log --stat 显示简略统计信息
    • git log --pretty=oneline
    • git log --pretty=format:"%h - %an, %ar : %s" --graph
    • git log --since=2.weeks|--until= 时间限制
    • git log -S function_name 显示添加或删除了该字符串的提交

撤销操作

  • git commit --amend
$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend
  •  git reset HEAD -- <file> 取消暂存
  • git checkout -- <file> 重置修改
  • Git中任何已提交的东西几乎都是可以恢复的。

远程仓库

  • git remote 列出远程仓库服务器的简写
    • git remote -v 显示需要读写远程仓库使用的Git保存的简写与其对应的URL
    • git remote add <shortname> <url> 添加远程仓库
    • git remote show <remote> 查看某个远程仓库
    • git remote rename <shortname> <newname> 重命名远程仓库
    • git remote remove|rm <shortname> 移除远程仓库
  • git fetch <remote> 从远程仓库获得数据
  • git push origin master 推送到远程仓库
  • git pull 抓取所有远程引用

打标签

  • git tag 列出已有标签
    • git tag -l "" 按照特定模式查找标签
    • git tag -a v1.4 -m "" 创建附注标签
    • git show v1.4 查看标签信息
    • git tag v1.4 创建轻量标签
    • git tag -a v1.2 9fceb02 给过去的提交打标签
  • 推送
    • git push origin <tagname> 推送标签到远程仓库
    • git push origin --tags 推送所有标签
  • 删除
    • git tag -d <tagname> 删除轻量标签
    • git push <remote> :refs/tags/<tagname>|git push <remote> --delete <tagname>
  • 检出标签
    • git checkout <tagname> 这会使得你的仓库处于“分离头指针”的状态

Git别名

通过git config文件来为每个命令设置别名。例子:

  • git config --global alias.cmt commit
  • git config --global alias.last 'log -1 HEAD'
  • git config --global alias.visual '!gitk'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值