本文最后更新于 2019-07-11【1746 天前】,文中所描述的信息可能已发生改变,请谨慎使用。如有问题或建议,欢迎在文章底部留言参与讨论!

git 是现在最流行的版本控制工具。与 CVS、Subversion 一类的集中式版本控制工具不同,它采用了分布式版本库的作法,不需要服务器端软件,就可以运作版本控制,使得源代码的发布和交流极其方便。git 的 速度很快,git 最为出色的是它的合并追踪( merge tracing )能力。

创建库

  • 克隆一个已存在的库

    $ git clone https://github.com/user/repo.git [DirName]
    或者
    $ git clone git@github.com:user/repo.git [DirName]

    其中,user 为用户名, repo 为需要克隆的库名,DirName 为可选,默认值为库的名称 repo

  • 新建本地库

    $ git init

本地修改

  • 查看本地工作目录下的文件变化

    $ git status
  • 已跟踪文件(tracked files)的变化

    $ git diff
  • 将本地所有修改加入到下次提交(commit)中:

    $ git add .
  • 把 file 文件中的改变加入到下次commit中:

    $ git add -p file
  • 提交跟踪文件在本地的修改:

    $ git commit -a 
  • 提交在工作台中的修改

    $ git commit
  • 修改最近一次的commit(不要修改已经published的commit)

    $ git commit --amend

Commit历史记录

  • 从最新的一次开始,罗列所有提交

    $ git log
  • 查看某一文件的所有commit记录

    $ git log -p file
  • 查看某一文件的修改记录-Who,When,What

    $ git blame file

分支与标签(Branch & Tag)

  • 列出所以存在的分支

    $ git branch -av
  • 把HEAD移到分支上(转到分支上)

    $ git checkout <branch>
  • 基于目前的HEAD创建新分支

    $ git branch <new-branch>
  • 创建全新的分支

    $ git checkout --orphan <branch>
  • 创建远程分支的跟踪分支

    $ git checkout --track <remote/branch>
  • 删除本地分支

    $ git branch -d <branch>
  • 给当前的commit打标签

    $ git tag <tag-name>

更新与提交

  • 列出所有远程仓库

    $ git remote -v
  • 查看远程仓库的信息

    $ git remote show <remote>
  • 添加名为的新远程库

    $ git remote add <shortname> <url>
  • 从远程库下载所有修改,但不要加入到HEAD中

    $ git fetch <remote>
  • 下载修改并合并/加入到 HEAD中

    $ git pull <remote> <branch>
  • 把本地修改推送(push)到远程库

    $ git push <remote> <branch>
  • 删除远程库上的分支

    $ git branch -dr <remote/branch>
  • push标签

    $ git push --tags
  • push某一具体标签

    $ git push <remote> <tag>

合并与编辑(merge & Rebase)

  • 合并分支到当前HEAD中

    $ git merge <branch>
  • 把当前HEAD变到分支上

    $ git rebase <branch>
    不要对已经push的commits进行rebase
  • 放弃rebase

    $ git rebase --abort
  • 解决冲突之后,仍rebase

    $ git rebase --continue
  • 使用自定义的merge工具来解决冲突

    $ git mergetool
  • 使用自定义的编辑器来手动解决冲突并在解决冲突之后标记为已解决

    $ git add resolved-file
    $ git rm resolved-file

撤销 Undo

  • 放弃在工作目录中的所有本地修改

    $ git reset --hard HEAD
  • 放弃某一文件中的本地修改

    $ git checkout HEAD file
  • 恢复 (revert) 某次 commit

    $ git revert <commit>
  • 重置 HEAD 指向某次 commit:并放弃之后的所有修改
  • ...并放弃之后的所有修改

    $ git reset --hard <commit>
  • ...并把之后的修改保存到unstaged changes

    $ git reset <commit>
  • ...并保留本地没有commit的修改:

    $ git reset --keep <commit>

最基础

git init [<Project-Name>]                       #默认当前目录,否则新建<Project-Name>这个目录
git add .                                       #把本地修改加入到暂存区
git commit -m "<commit-message>'                #提交本次修改
git remote add origin <remote-repo>             #添加远程库
git push origin <local-branch>:<remote-branch>  #push到远程

最后!

神器:Help命令~

$ git help <command>
文章目录


推荐使用:阿里云 云翼计划学生优惠、ECS、轻量应用等产品与服务【 点击注册

本文作者:Quanyin Tang

本文链接:Git 常用命令 - https://www.imtqy.com/git-cheet-sheet.html

版权声明:如无特别声明,本文即为原创文章,仅代表个人观点,版权归 Quanyin 所有,未经允许禁止转载,经授权转载请注明出处!