git

Git总结

2020-01-11  本文已影响0人  redher

初步

  1. 下载git
  2. 基础配置:
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

基础操作

获取Git仓库

  1. 初始化现有仓库

git init

  1. 克隆远程仓库

git clone [url]

查看工作目录状态

git status

添加文件到暂存区

git add files

提交暂存区中的文件生成一次快照

  1. 以默认模式提交,会自动打开一个默认编辑器

git commit

  1. 一默认模式提交,自动打开默认编辑器并在注释中显示文件的区别

git commit -v

  1. 跳过暂存区直接提交

git commit -a

  1. 不打开编辑器直接填写提交信息

git commit -m "message"

忽略文件

编辑.gitignore文件来实现

对比修改的内容

  1. 对比工作区与暂存区中的内容

git diff

  1. 对比暂存区与版本快照中的内容

git diff --cached

移除文件

  1. 移除文件

git rm files

  1. 移除版本库中的文件但是保留工作目录下的文件

git rm --cached files

移动文件

  1. 移动/重命名操作

git mv file new_file

  1. 等同操作

mv file new_file
git rm file
git add new_file

撤销操作

重新提交

git commit --amend

撤销对文件的修改(使用版本库文件覆盖工作区文件)

git restore <file> # 2.23版本及以后
git checkout -- <file> # 2.23版本前

取消暂存的文件

git restore --staged <file> # 2.23版本及以后
git reset HEAD <file> # 2.23版本前

远程仓库

生成SSH key

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

查看远程仓库

  1. 查看git版本库所有关联的远程仓库的名字和地址

git remote

  1. 查看某个远程仓库的详细信息

git remote show [remote_name]

添加远程仓库

git remote add <shortname> <url>

常用的两种传输协议:

  1. SSH传输协议:

git@github.com:<user>/<project_name>

  1. HTTP协议:

https://github.com/<user>/<project_name>

注意:无论哪种协议project_name后面都有后缀名.git

推送到远程仓库

git push [remote_name] [branch_name]

从远程仓库中抓取与拉取

  1. 仅仅拉取数据,不改变当前工作内容

git fetch [remote_name]

  1. 抓取数据并合并到当前分支

git pull [remote_name]

远程仓库重命名与移除

  1. 重命名

git remote rename [remote_name] [new_remote_name]

  1. 删除

git remote rm [remote_name]

分支

本地分支操作

  1. 列出当前git仓库的所有分支

git branch

  1. 列出当前git仓库所有分支的最新一次提交

git branch -v

  1. 列出已经合并到或尚未合并到当前分支的分支

git branch --merged
git branch --no-merged

  1. 创建一个新分支

git branch branch_name

  1. 切换到一个分支上

git checkout branch_name

  1. 创建并切换到这个分支上

git branch -b branch_name

  1. 将一个分支合并到当前分支

git merge branch_name

  1. 删除一个已经被合并的分支

git branch -d branch_name

  1. 强制删除一个未合并的分支

git branch -D branch_name

  1. 变基

git rebase branch_name

远程分支的一些操作

  1. 克隆仓库,创建一个本地master跟踪分支指向origin/master远程分支

git clone url

  1. 同步远程分支到本地,不合并以及改变工作目录内容

git fetch remote_name

  1. 同步远程分支并与本地分支合并相当于git fetch后跟git merge

git pull remote_name

  1. 在远程分支上创建一个remote_branch_name远程分支并且将本地branch_name分支内容推送上去

git push remote branch_name:remote_branch_name

  1. 将远程分支合并到当前分支

git merge remote/remote_branch_name

  1. 新建一个远程跟踪分支来跟踪一个远程分支

git checkout -b branch_name remote/remote_branch_name

  1. 设置当前分支跟踪远程分支

git branch -u remote/remote_branch_name

  1. 列出所有远程跟踪分支以及一些详细信息

git branch -vv

  1. 从服务器上删除一个分支

git push remote --delete remote_branch_name

上一篇 下一篇

猜你喜欢

热点阅读