- MingW64
$ cat >>.bashrc<<EOF > alias gitlog='git log --graph --pretty=oneline --abbrev-commit' > EOF $ source .bashrc
git 版本控制的一些使用说明 |
git 知识笔记
官网
https://github.com/git
书目
概念
标签 tag
list
git tag
create
git tag -a v1.4 -m "my version 1.4"
sharing
git push origin --tags
Checking out
git checkout 2.0.0
delete
1
git tag -d v1.4-lw
git push origin :refs/tags/v1.4-lw
2
git push origin --delete <tagname>
分支 branch
查看远程分支
git branch -r
查看所有分支
git branch -a
创建
创建远程origin的dev分支到本地
$ git checkout -b dev origin/dev
在当前分支上创建并切换分支 git checkout -b branchB
push
full
git push origin <newbranch>
short
git push --set-upstream origin <newbranch>
git push
delete
delete local
git branch
git branch -d some_branch
delete remote branch
git branch -r
git branch -d origin/<branch_name>
切换分支 git checkout branchA
合并
将某分支合并到当前分支上 git merge branchA
baseBranch=some_base
workBranch=some_dev
git pull = git fetch + git merge
模型
远程 Remote Repository
git remote
git remote -v
git fetch
git remote remove origin
git remote add origin
远程到本地
数据交换
git clone
git pull
git pull = git fetch + git merge
git fetch
git merge
branch
$ git branch -r 查看远程
本地
tree -d .git
只读远程镜像库 Repository/远程副本
本地到远程
数据交换
git rebase
git push
git hub : pull request 由github等平台 提供
工作区
workspace
index
操作
$ git add
git add .
git add [files]
git add ./*.filetype
git commit -m "xxxx"
git revert
1.
git revert HEAD
git revert 6c1f9c86e4a97109662b9ae442421aff4ecd30fc
2.
git push origin HEAD
get reset [file]
$ git branch
状态
git status
git diff
$ git diff [file]
git blame <somefile>
git log
git log
git log origin/master
git show
git show
git show 160250104756b2b2ada6ded4bf778557a7ae9bd2
git show origin 160250104756b2b2ada6ded4bf778557a7ae9bd2
暂存库 stash
git stash
git stash save
git stash list
git stash show
git stash pop
git stash apply
约定式提交
commit message格式
<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>
<类型>[可选的作用域]: <描述>
[可选的正文]
[可选的脚注]
git常用命令速查表
入门
廖雪峰的Git教程
http://www.liaoxuefeng.com/
备忘条
背景知识
version control system
版本控制系统分类
集中式
CVS
SVN
ClearCase
VSS
分布式
BitKeeper
Mercurial
Bazaar
git
https://www.toutiao.com/a6797270777075859971 Git 高级用法
实战
常用操作
复制 clone
git clone https://xxxxxx.git
equals
git init
git remote add origin https://xxxx.git
git branch --set-upstream-to=origin/master master
git stash
git merge
日志
git log
alias gitlog='git log --graph --pretty=oneline --abbrev-commit' 注意:到备注里复制
$ cat >>.bashrc<<EOF > alias gitlog='git log --graph --pretty=oneline --abbrev-commit' > EOF $ source .bashrc
拉取 pull/fetch
git pull
自动前向合并 merge,遇到冲突,自动标识出来
git fetch 不自动merge 更安全
git fetch origin master
git log -p master..origin/master
git merge origin/master
等同于 git fetch origin master:tmp
git diff tmp
git merge tmp
git fetch
推送
git push
git push origin HEAD
git push origin branch AAA
ssh
ssh key
keygen
ssh-keygen -t rsa -b 4096 -C "who@whatever.com"
default:id_rsa
others
run ssh-agent background
eval $(ssh-agent -s)
add key
ssh-add ~/.ssh/who_at_whatever_dot_com_rsa
cd ~/.ssh
vi config
#
Host git.xxx.com
HostName git.xxx.com
User git
AddKeysToAgent yes
IdentityFile ~/.ssh/id_rsa
#
Host git.yyy.com
HostName git.yyy.com
User git
AddKeysToAgent yes
IdentityFile ~/.ssh/who_at_whatever_dot_com_rsa
# Personal GitHub account
Host github.com
HostName github.com
User git
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa
# Work GitHub account
Host github.com-work
HostName github.com
User git
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/work_rsa
git clone git@github.com:[pubnamespace]/[project].git
git clone git@github.com-work:[my work GitHub group]/[myproject].git
Tortoise Git
使用git ssh key
TortoiseGit - Settings -Networks - ssh client
Path\TortoiseGit\bin\TortoiseGitPlink.exe
变更为 Path\Git\usr\bin\ssh.exe
减少垃圾提交
有风险
风险操作
$ git reset
$ git reset --hard
git rebase
高风险
应用场景
合并多次提交纪录
git rebase -i --root # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup <commit> = like "squash", but discard this commit's log message # x, exec <command> = run command (the rest of the line) using shell # b, break = stop here (continue rebase later with 'git rebase --continue') # d, drop <commit> = remove commit # l, label <label> = label current HEAD with a name # t, reset <label> = reset HEAD to a label # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>] # . create a merge commit using the original merge commit's # . message (or the oneline, if no original merge commit was # . specified). Use -c <commit> to reword the commit message. ---- git rebase -i --root git add/rm <conflicted_files> git rebase --continue git rebase --skip
合并最近的 4 次提交纪录
1.
git rebase -i HEAD~4
2
p, pick 选择 = use commit
r, reword 重写备注 = use commit, but edit the commit message
e, edit = use commit, but stop for amending 修订
s, squash 塞进 = use commit, but meld 合并 into previous commit
f, fixup = like "squash", but discard 丢弃 this commit's log message
x, exec = run command (the rest of the line) using shell
d, drop = remove commit
3
4
5
查看结果
git log
人工交互
分支合并
1
2
other
git push origin master
git stash
git checkout master
git pull master
git checkout feature1
git merge master
git log
git rebase master
首先,git 会把 feature1 分支里面的每个 commit 取消掉;
其次,把上面的操作临时保存成 patch 文件,存在 .git/rebase 目录下;
然后,把 feature1 分支更新到最新的 master 分支;
最后,把上面保存的 patch 文件应用到 feature1 分支上;
3.
4
救命稻草
git rebase —abort
对一个分支做「变基」操作
六、为什么会是危险操作?
revert one file change
git status
git reset
git checkout filename
advance
某分支上的修改迁移到另一分支
cherry-pick commit-id
更换源
$ git remote -v
$ git remote remove origin
$ git remote add origin git@git.xxx.com/s.git
$ git remote -v
$ git pull
git branch --set-upstream-to=origin/<branch> master