关于前端:Git常用命令和多账号配置

1次阅读

共计 4943 个字符,预计需要花费 13 分钟才能阅读完成。

团队合作开发中少不了版本控制管理工具,因为近期公司我的项目从 SVN 更换成 Git,这里次要做一个温习总结。SVNGit 最次要要的区别:SVN 是集中式的,Git 是分布式的。Git 的劣势在于易于本地减少分支和分布式的个性,可离线提交,解决了异地团队协同开发等 SVN 不能解决的问题。

Git 最外围的一个概念就是工作流。

  • 工作区 (Workspace) 是电脑中理论的目录。
  • 暂存区 (Index) 相似于缓存区域,长期保留你的改变。
  • 仓库区 (Repository),分为本地仓库和近程仓库。

常用命令速查

先来偷个图(图片来源于网络):

初始化

# 在当前目录新建一个 Git 代码库
$ git init

# 新建一个目录,将其初始化为 Git 代码库
$ git init [project-name]

# 下载一个我的项目和它的整个代码历史 [Git only]
$ git clone [url]

配置

# 列举所有配置
$ git config -l

# 为命令配置别名
$ git config --global alias.co checkout
$ git config --global alias.ci commit
$ git config --global alias.st status
$ git config --global alias.br branch

# 设置提交代码时的用户信息
$ git config [--global | --local] user.name "[name]"
$ git config [--global | --local] user.email "[email address]"

减少 / 删除 / 批改文件

# 增加指定文件到暂存区
$ git add [file1] [file2] ...

# 增加指定目录到暂存区,包含子目录
$ git add [dir]

# 增加当前目录所有文件到暂存区
$ git add .

# 删除工作区文件,并将这次删除放入暂存区
$ git rm [file1] [file2] ...

# 进行追踪指导文件,但不删除
$ git rm --cached [file]

# 文件改名,并放入暂存区
$ git mv [old] [new]

提交

# 提交暂存区到仓库区
$ git commit -m [message]

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

# 提交工作区自上次 commit 之后变动,间接到仓库区
$ git commit -a

# 提交时显示所有 diff 信息
$ git commit -v

# 应用一次新的 commit,代替上一次提交;若代码无变动,则改写上次 commit 的提交信息
$ git commit --amend -m [message]

# 重做上一次 commit,并包含指定文件的新变动
$ git commit --amend [file1] [file2]

分支

# 显示所有本地分支
$ git branch

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

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

# 新建分支,但停留在以后分支
$ git branch [branch-name]

# 新建分支,与指定近程分支建设追踪关系
$ git branch --track [branch] [remote-branch]

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

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

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

# 切换到指定分支,并更新工作区
$ git checkout [branch-name]

# 切换到上一个分支
$ git checkout -

# 建设追踪关系,在现有分支与指定的近程分支之间
$ git branch --set-upstream [branch] [remote-branch]

# 合并指定分支到以后分支
$ git merge [branch]

# 衍合指定分支到以后分支
$ git rebase [branch]

# 合并一个 commit 到以后分支
$ git cherry-pick [commit]

标签

# 列出所有本地标签
$ git tag

# 基于最新提交创立标签
$ git tag [tag]

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

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

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

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

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

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

查看信息

# 显示状态
$ git status

# 显示以后分支的版本历史
$ git log

# 显示 commit 历史,以及每次 commit 产生变更的文件
$ git log --stat

# 搜寻提交历史,依据关键词
$ git log -S [keyword]

# 显示某个文件的版本历史,包裹文件改名
$ git log --follow [file]
$ git whatchanged [file]

# 显示指定文件相干的每一次 diff
$ git log -p [file]

# 显示过来 5 次提交
$ git log -5 --pretty --oneline

# 显示所有提交过的用户,按提交次数排序
$ git shortlog -sn

# 显示指定文件是什么人在什么工夫批改过
$ git blame [file]

# 查看某人提交记录
$ git log --author=[username] 

# 显示暂存区和工作区差别
$ git diff

# 显示暂存区和上一个 commit 差别
$ git diff --cached [file] 

# 显示工作区和以后分支最新 commit 之间的差别
$ git diff HEAD

# 查看某次提交具体批改内容
$ git show [commit]

# 显示某次提交发生变化的文件
$ git show --name-only [commit]

# 显示某次提交,某个文件的内容
$ git show [commit]:[file]

# 显示以后分支最近几次提交
$ git reflog

近程操作

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

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

# 取回近程仓库的变动,并与本地分支变基合并
$ git pull --rebase [remote] [branch]

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

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

# 减少一个新的近程仓库,并命名
$ git remote add [remote-name] [url]

# 上传本地指定分支到近程仓库
$ git push [remote] [branch]

# 强行推送以后分支到近程仓库,即便有抵触
$ git push [remote] --force

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

撤销

# 复原暂存区的指定文件到工作区
$ git checkout [file]

# 复原暂存区当前目录的所有文件到工作区
$ git checkout .

# 复原工作区到指定 commit
$ git checkout [commit]

# 重置暂存区的指定文件,与上一次 commit 保持一致,但工作区不变
$ git reset [file]

# 重置暂存区与工作区,与上一次 commit 保持一致
$ git reset --hard

# 重置以后分支的指针为指定 commit,同时重置暂存区,但工作区不变
$ git reset [commit]

# 重置以后分支的 HEAD 为指定 commit,同时重置暂存区和工作区,与指定 commit 统一
$ git reset --hard [commit]

# 撤销工作目录中所有未提交文件的批改内容
$ git reset --hard HEAD

# 新建一个 commit,用于撤销指定 commit,后者所有变动将被前者对消,并利用到以后分支
$ git revert [commit]

# 将未提交的变动放在储备区
$ git stash

# 将储备区的内容复原到当前工作区
$ git stash pop

多账号配置

有时候咱们本人有 github 的账号作为集体应用,公司团队应用 gitlab 另一账号,这时咱们就须要对同一设施配置多账号。

一、生成 ssh 密钥

别离对 githubgitlab 生成对应的密钥(默认状况下本地生成的秘钥位于 /Users/ 用户名 /.ssh/),并且配置 git 拜访不同 host 时拜访不同的密钥,流程如下:

  1. 在 gitbash 中应用 ssh-keygen -t rsa -C "公司邮箱地址" 生成对应的 gitlab 密钥:id_rsaid_rsa.pub
  2. 将 gitlab 公钥即 id_rsa.pub 中的内容配置到公司的 gitlab 上
  3. 在 gitbash 中应用 ssh-keygen -t rsa -C "github 邮箱地址" -f ~/.ssh/id_rsa.github 生成对应的 github 密钥:id_rsa.githubid_rsa.github.pub
  4. 将 github 公钥即 /id_rsa.github.pub 中的内容配置到本人的 github 上
  5. 进入密钥生成的地位,创立一个 config 文件,增加配置:

    Host gitlab                            #域名地址的别名
        HostName gitlab.com                #这个是实在的域名地址
        User gitlab                        #配置应用用户名
        IdentityFile ~/.ssh/id_rsa.github  #这里是 id_rsa 的地址   
    Host github
        HostName github.com
        User github
        IdentityFile ~/.ssh/id_rsa

二、测试

在密钥的生成地位 /Users/ 用户名 /.ssh/ 下应用 gitbash 运行 ssh -T git@hostName 命令测试 sshkeygitlabgithub 的连贯:

ssh -T git@gitlab
# 如果配置正确会提醒
Welcome to GitLab, @gitlab! 

ssh -T git@gihub
# 如果配置正确会提醒
Hi github! You've successfully authenticated, but GitHub does not provide shell access.

三、配置 git 仓库

gitconfig 文件记录了用户的根本信息,咱们的账号信息也在外面,这里咱们要做的就行在不同的本地仓库配置不同的用户信息来拜访不同的近程仓库

config 文件通常有三个地位:

  • system(零碎级别) 位于 Windows 下在 git 的装置目录,蕴含了实用于零碎所有用户和所有库的值。如果你传递参数选项 --systemgit config,它将明确的读和写这个文件。
  • global(用户级别) 位于 ~/.gitconfig,具体到你的用户。你能够通过传递 --global 选项使 Git 读或写这个特定的文件。
  • local(仓库级别) 位于 .git/config,无论你以后在用的库是什么,特定指向该繁多的库优先级最高。

用户级别配置

因为公司的代码应用频率较高,所以将 git 配置文件的 global(用户级别)设置为公司的 gitlab 账号:

$ git config --global user.name "gitlab" // 公司账号名称
$ git config --global user.email "gitlab@gitlab.com" // 公司账号邮箱 

仓库级别配置

local(仓库级别)配置成 github 的账号。此时须要先 init 一个 git 的仓库并进入外面后执行如下命令:

$ git config --local user.name "github" // github 账号名称
$ git config --local user.email "github@github.com" // github 账号邮箱 

四、应用

# 之前的形式: 单个账号
git clone git@github.com:xxxx/xxx.git #缺省 config 配置时
git clone git@github:xxxx/xxx.git #config 配置后,等价于第一条语句

# 当初要改为,git clone git@域名别称:xxxx/xxx.git
# 就是应用域名地址的别名来辨别
git clone git@github:xxxx/xxx.git
git clone git@gitlab:xxxx/xxx.git
正文完
 0