git常用命令

git是世界上最先进的分布式版本控制系统。git是有linux之父linus仅仅用了两周时间开发的,果然牛人就是这么牛!git非常流行,会用git是我们在开发中必备的技能。

一.分布式VS集中式

分布式:

  • git是分布式版本控制系统;
  • 每台电脑都是完整的仓库
  • 无需联网就能在本地工作

集中式:

  • CVS和SVN是常用的集中式版本管理系统
  • 最新仓库存储在中央服务器
  • 需要联网才能从中央服务器获取最新版内容

二.几个区的概念

1.工作区(Working Directory):电脑本地可以看见的文件目录
2.版本库(Repository):文件目录下隐藏的.git文件目录,包括暂存区(index/stage)和分支(默认分支master)
3.远程仓库(Origin):这里是github仓库

三.配置信息

Git的设置文件在gitconfig中,在用户名下配置是全局配置,在某个文件目录下配置是局部配置。

# 配置全局用户名和邮箱
$ git config --global user.name “your name”
$ git config --global user.email “your email”

# 查看配置信息
$ git config --list

四.建仓库

# 初始化仓库
$ git init

#从远程仓库中克隆
$ git clone [url]

五.文件管理

  • 把工作区的文件添加到暂存区
# 添加指定文件到暂存区
$ git  add [file1]  [file2]...

# 添加指定目录到暂存区
$ git  add  [dir]

# 添加当前目录的所有文件到暂存区
$ git  add  .
  • 把暂存区的文件提交到仓库区
# 提交暂存区到仓库区
$git  commit -m  [message]

# 提交暂存区的指定文件到仓库区
$ git  commit  [file1][file2]...  -m  [message]

# 提交时显示所有diff信息
$ git  commit -v
  • 查看信息
# 查看仓库的当前状态
$ git  status

# 查看修改内容
$ git diff

# 查看提交历史
$ git log

# 查看命令历史
$ git  reflog

# 查看今天提交了多少行代码
$ git diff --shortstat “@{0 day ago}”
  • 修改
# 删除版本库中的文件,并且commit

$ git  rm  [file][file2]...
$ git  commit -m  [file]

# 从版本库中恢复错删文件
$ git  checkout  --[file]

# 倒退到指定版本
$ git reset --hard  commit_id

#  改名文件,并且将这个改名放入暂存区
$  git mv [file-original] [file-renamed]

# 暂时将未提交的变化移除,然后再恢复
$ git stash
$ git stash pop

六. git与 github

# 获取SSH Keys
$ssh-keygen -t rsa -C “youremail”

# 关联远程仓库,shortname一般是origin
$ git remote add [shorname][url]

# 显示所有远程仓库
$ git remote -v

# 下载远程仓库的所有变动
$ git fetch [remote]

# 显示某个远程仓库的信息
$ git remote show [remote]

# 取回远程仓库的变化,并与本地分支合并
$ git  pull [remote][branch]

# 推送某个分支到远程仓库,第一次有u
$ git push  [-u][remote][branch]

# 推送所有分支到仓库
$ git push [remote]--all

七.分支

# 列出本地所有分支,*表示当前head指向
$ git branch

# 列出所有远程分支
$ git branch -r

# 列出所有本地分支和远程分支
$ git branch -a

# 新建分支,指向还没有变
$ git branch [branch-name]

# 新建分支,并且切换到该分支
$ git checkout -b [branch-name]

# 切换到指定分支
$ git checkout  [branch-name]

# 合并指定分支到当前分支
$ git merge [branch-name]

# 删除分支
$ git branch -d [branch-name]

# 删除远程分支
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

八.标签

# 列出所有标签
$ git tag

# 添加一个标签到当前commit
$ git tag [tag-name]

# 添加一个标签到指定commit
$ git tag [tag-name][commit]

# 删除本地标签
$ git tag -d [tag-name]

# 删除远程标签
$ git push origin :refs/tags/[tag-name]

# 查看标签信息
$ git show [tag-name]

# 提交指定标签
$ git push [remote][tag-name]

#  提交所有tag
$  git push [remote] --tags

#  新建一个分支,指向某个tag
$  git checkout -b [branch] [tag]

From:https://zhuanlan.zhihu.com/p/25794746
原文地址:https://www.cnblogs.com/timlong/p/7953719.html