git自用手册

Posted on Jun 1, 2017
整理了一下git常用命令以及使用常遇到的场景, 作为一个简单的手册方便查阅

1. 常用设置

# git alias setting
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
git config --global alias.st status
git config --global alias.ci commit
git config --global alias.cm commit
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cy cherry

git config --global user.name "nick"
git config --global user.email "[email protected]"
git config --global core.editor vim

# git proxy setting
git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'
git config --global --unset http.proxy
git config --global --unset https.proxy

git config http.proxy 'socks5://127.0.0.1:1080'
git config https.proxy 'socks5://127.0.0.1:1080'
git config --unset http.proxy
git config --unset https.proxy

# https 方式免密码提交
git config --global url."ssh://[email protected]:acme-corporation".insteadOf "https://github.com/acme-corporation"
git config --global url."[email protected]:".insteadOf "http://gitlab.xxxx.com/"
# 存储上次输入的密码
git config --global credential.helper store

2. 常用命令

# 克隆远端分支
git clone [email protected]:<user>/<repo>.git

# 切换到master分支
git checkout master

# 更新当前分支
git pull

# 合并master到当前分支
git merge master

# 修改注释 & 追加到最近的commit
git commit --amend

# 提交代码到git
git push 

# 更新子模块代码
git submodule update --init --recursive

# 添加一个子仓库
git submodule add [email protected]:<user>/<repo>.git submodule-dir

3. TAG操作

# 切换到TAG VER-2-9-1
git checkout -b com_br VER-2-9-1

# 在指定commit上打tag
git tag -a V1.2.0.20170429.fix 96fe737
git push origin V1.2.0.20170429.fix

# 删除tag
git tag -d V1.1.0.20170429.fix

# 修改tag
git tag -a V1.1.0.20170429.fix -m 'version 1.4'

4. 远程分支

# 在当前分支下创建my-test的本地分支分支
git checkout -b my-test

# 将my-test分支推送到远程
git push origin my-test

# 将本地分支my-test关联到远程分支my-test上
git branch --set-upstream-to=origin/my-test

# 查看分支关联关系
git branch -vv

# 删除分支
git branch -d my-test

# 删除远端分支
git push origin --delete my-test

5. 冲突解决

git checkout --ours index.html
git checkout --theirs _layouts/default.html
git mergetool -t vimdiff
git add index.html
git commit -m "merge"

6. 藏储(stash)

git stash
git stash list    -- 查看藏储列表
git stash apply [stash@{2}] --切换到藏储
git stash drop    移除
git stash pop    重新应用
git stash branch testchanges 从当前藏储创建分支
git stash clear        //清除全部stash
git stash drop stash@{0}    //清除一个

7. 回滚commit

git cherry -v
git reset --hard HEAD~1 # 撤销最后一次未push的commit(慎用-会直接删除提交)
git reset --hard b7057a9 # 跳到某个commit
git reset --soft HEAD^ # 撤销所有commit,保留代码
git reflog  # 查看reset日志

8. 放弃本地修改(强制更新还原分支)

# 只是下载远程的库的内容,不做任何的合并
git fetch --all
# 把HEAD指向刚刚下载的最新的版本
git reset --hard origin/master 

9. 连接到已存在仓库

git remote add origin [email protected]:nc-note/fortune-data.git
git branch -M main
git push -u origin main

10. 迁移仓库

git remote set-url --push origin [email protected]:<user>/<repo>.git

# 可以解决[致命错误:拒绝合并无关的历史] 问题
git pull origin main --allow-unrelated-histories

git branch -M main
git push -u origin main

11. 日志

git log --tags --simplify-by-decoration --pretty="format:%ci %an %d"
git log --pretty=oneline
git log --author="<user>"  --after="2017-05-21 00:00:00" --before="2017-05-25 23:59:59"
git log --pretty=format:"%H-%cn-%s-%cd" --author="<user>"

# 查看修改文件(从指定的commit开始)
git log --name-status e18dccdde0ce741b5bb744f50e5212a87fb56230

# 查看修改内容
git show e18dccdde0ce741b5bb744f50e5212a87fb56230 fileName

git log --pretty=format:"arg"
    %H 提交对象(commit)的完整哈希字串
    %h 提交对象的简短哈希字串
    %T 树对象(tree)的完整哈希字串
    %t 树对象的简短哈希字串
    %P 父对象(parent)的完整哈希字串
    %p 父对象的简短哈希字串
    %an 作者(author)的名字
    %ae 作者的电子邮件地址
    %ad 作者修订日期(可以用-date= 选项定制格式)
    %ar 作者修订日期,按多久以前的方式显示
    %cn 提交者(committer)的名字
    %ce 提交者的电子邮件地址
    %cd 提交日期
    %cr 提交日期,按多久以前的方式显示
    %s 提交说明