Talk about Git

Posted by aimyew on 2018-08-27

This is a personal note. If there are any mistakes in writing, please email me. Git is a skillfull tool that beginners must learn. Of course, SVN is also a usefull tool. The actual management requirements of various projects in the company are different, so we need master it.

Quick Start

setting info

  • $ git config –global user.name “aimu_zs”
  • $ git config –global user.email “1422203188@qq.com
  • $ git config –unset user.name
  • $ ssh-keygen -t rsa -C “1422203188@qq.com

check info

  • $ git config –system -list 或者 -l
  • $ git config –global -l

Config Level

  • system level
    • /etc/gitconfig 系统级别的配置文件,针对所有用户,对应 git config –system 选项
  • global level
    • ${HOME}/.gitconfig 用户级别的配置文件,针对当前用户,对应 git config –global 选项
  • normal project level
    • .git/config 项目级别的配置文件,针对当前项目,对应 git config
  • 作用强度:normal > global > system

others(optional | also can configure these in file [D:\Git\mingw64\etc\gitconfig] )

  • $ git config –system core.editor=’d:\notepad++’
  • $ git config –global color.ui auto
  • $ git config –global color.status auto
  • $ git config –global color.branch auto
  • $ git config –global color.diff auto
  • $ git config –global color.interactive auto
  • $ git config –global alias.ii info
  • $ git config –help

单机下Git多账户

1、touch C:\Users\wangyuan.ssh\config

2、config:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Default github user(xx@mail.com)
Host github.com
HostName github.com
IdentityFile C:/Users/wangyuan/.ssh/id_rsa_github
User userNameFoo
PreferredAuthentications publickey

# Default github user(xx@mail.com)
Host github.com
HostName git.oschina.net
IdentityFile C:/Users/wangyuan/.ssh/id_rsa_osa
User userNameBar
PreferredAuthentications publickey

# PreferredAuthentications可设为publickey,password publickey,keyboard-interactive等

常用CMD

1
2
3
4
5
6
7
8
9
烂大街,必须会
$ git add .
$ git commit -m "give some word to explain this operation"
$ git push origin master

或者

$ git commit -am "give some word to explain this operation"
$ git push origin master
1
2
3
4
5
6
7
$ git checkout -b dev 新建一个本地分支 dev
$ git push -u origin dev 创建远端分支 dev
$ git commit -am "origin-dev" 操作信息 commit
$ git push origin dev 把本地 dev 分支推到 origin


$ git branch --set-upstream branch-name origin/branch-name 如果需要,则建立本地分支与远端分支的关联
1
2
3
4
git status 查看当前目录的文件状态
workingDirectory (untracked)
stagingStatus (unmodified modified)
gitRepos (staged)
1
2
3
4
5
git checkout 切分支和撤销更改
$ git checkout -- file 撤销工作区对 file 的修改
$ git checkout . 把当前工作区中没有 add/commit 的所有文件恢复至上一次 add/commit 时的状态
$ git checkout dev 切换到本地 dev 分支
$ git checkout -b dev 创建并且换到本地 dev 分支
1
2
3
4
5
git branch 查看当前所有分支,并且对当前分支标记 ⁎ 号
$ git branch dev 创建本地 dev 分支
$ git checkout -b dev 创建并切换到本地 dev 分支
$ git branch -d dev 删除本地 dev 分支
$ git branch -D feature 若从 dev 来的新功能分支 feature 还没有 merge 进 dev 就需要被删除,则使用 -D 来执行
1
2
3
4
5
6
git remote 远端分支
$ git remote 列出已存在的分支
$ git remote --verbose 或者 -v 查看当前
$ git remote add origin https://github.com/AimuZsGitHub/tryGit.git 把当前 local master 推到 origin (这句话实际在说:local master branch add a remote branch named origin and urld https...)
$ git remote add origin git@github.com:AimuZsGitHub/tryGit.git 不需要 authorization
$ git remote get-url origin 查看当期远端叫作 origin 的地址
1
2
3
git merge 合并
$ git merge dev 将 dev 的开发产品合并到当前分支
$ git merge --no-ff -m "mergeBranchWithACommit" dev 用这种方式合并,则会有一次 commitId (但是自己测试中,直接 git merge dev 也是有 commitId 的,各位大能,如有阅读,敬请指教 ! )
1
2
git fetch >>>
$ git fetch >>>
1
2
git rebase >>>
$ git rebase >>>
1
2
git pull 从远端拉内容到本地
$ git pull origin master 从远端拉内容
1
2
3
4
5
6
7
8
9
10
git stash 储存当前工作现场状态以便切换到其他分支工作
$ git stash list 查看所有被储存的工作现场

$ git stash list 查看所有
$ git stash apply stash@{0} 恢复指定stash
$ git stash drop stash@{0} 删除工作状态

或者

$ git stash pop 恢复到上一次 stash 并删除该 stash
1
2
3
 git log 展示 commit log
$ git log -1 查看最后一次 log 信息
$ git log --graph 查看分支合并图
1
2
3
4
5
6
7
8
9
 git tag 打标签(标签建立在 commitId 之上)
$ git tag 查看本地所有的标签
$ git show v0.0.0-release 查看 0 号标签信息
$ git tag v0.0.1-release 为当前分支的内容打 1 号标签
$ git tag v0.0.2-release commitID 为某次 commitID 打 2 号标签
$ git tag -a v0.0.3-release -m "explain to this tag" commitID 创建说明性标签 -a指定标签名是 3 号标签,-m指定说明文字
$ git tag -d v0.0.4-release 删除 4 号标签
$ git push origin v0.0.5-release 上传 5 号标签到远端
$ git push origin :refs/tags/v0.0.6-release 删除远端 6 号标签,