记录Git操作的命令和细节
Command git pull & git fetch git fetch
是将远程主机的最新内容拉到本地,用户在检查了以后决定是否合并到工作本机分支中;而git pull
则是将远程主机的最新内容拉下来后直接合并,git pull = git fetch + git merge
git diff
仅查看每个文件更改的行数 git diff --stat
查看对比前 n 个 commit 的更改 git diff HEAD~n
git add
将文件的修改,文件的新建,添加到暂存区 git add .
将文件的修改、文件的删除,添加到暂存区 git add -u
将文件的修改,文件的删除,文件的新建,添加到暂存区 git add -A
git commit
撤销commit git reset --soft HEAD^
添加文件并提交commit git commit -a -m 'message'
修改未提交commit信息 git commit --amend -m
git push
推送本地分支到远程仓库 git push origin develop
git checkout 1 2 3 4 5 6 7 8 9 git checkout -b develop origin/develop git checkout -t origin/develop git checkout develop git checkout xx.file git checkout -- xxx.file git checkout .
git branch 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 git branch -r git branch -d a git branch -D a git branch -r -d origin/dev git push origin :dev git branch -m dev develop
git merge
合并远程分支到本地 git merge origin/beta
git stash
暂存代码 使用该命令可以将现在的代码保存下来并恢复到最近一次拉取下来的代码状态,可以使用save “last message”可以为暂存的代码添加备注信息 git stash [push -m "last message"]
恢复暂存代码 使用该命令可以将最近一次暂存代码取出和当前的代码进行合并 git stash pop
查看暂存代码列表 git stash list
取出指定版本的暂存代码 git stash apply stash@{1}
查看stash
的内容和当前内容的差异(-p
详细差异) git stash show [-p]
git tag
创建 git tag -a v1.9.13 -m "my version 1.9.13"
显示tag信息 git show v1.9.13
提交 git push origin v1.9.13
删除标签 git tag -d v1.9.13
删除本地标签后删除远程仓库标签 git push <remote> :refs/tags/<tagname>
rollback 1 2 git reset --hard HEAD^ git push origin HEAD --force
Others
查看文件修改状态git status -s
查看代码作者1 2 git blame [filename] git blame -L startLineNum, endLineNum
添加远程仓库git remote add origin git@github.com:stormzhang/test.git
生成ssh密匙ssh-keygen -t rsa
添加git用户信息git config --global user.name "smileorigin"
git config --global user.email "smileorigin@foxmail.com"
合并指定提交git cherry-pick <id>
gitignore 本地仓库的文件忽略规则可以在【本地仓库/.git/info/exclude】 文件中添加。注意,忽略规则不会提交到共享库中,只对本地有效
工程化 git commit风格
build
:构建相关
ci
:ci 相关
chore
:构建过程或辅助工具的变动
docs
:文档修改
feat
:新功能
fix
:修复 bug
perf
:改进性能的代码
refactor
:重构
revert
:版本回退
style
:格式改动
test
:增加测试
git 分支管理参考
源分支
目标分支
源分支描述
源分支从哪个分支切出
develop
N/A
基于此分支开发功能,为项目的开发分支,版本迭代应当基于此分支
master
master
develop
基于此分支发布文件,为项目的线上分支,Hotfix 应当基于此分支
N/A
feature/*
develop
用于集成功能
develop
hotfix/*
master
用于热修复补丁
master
配置 git 免登录提交 1 git clone 的时候,记得 clone ssh 的链接
vimdiff 1 2 3 git config --global diff.tool vimdiff git config --global difftool.prompt false git config --global alias.d difftool
git lg配置 – 更合理的 git log 显示 1 git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
使用后只要在命令行输入git lg
就可以查看到日志,效果比原来的git log
好太多了
Git 软件版本升级 1 2 3 4 5 6 7 8 9 10 11 sudo apt-get autoremove git sudo apt install make libssl-dev libghc-zlib-dev libcurl4-gnutls-dev libexpat1-dev gettext unzip wget https://www.kernel.org/pub/software/scm/git/git-2.21.0.tar.gz tar -xvf git-2.21.0.tar.gz cd git-2.21.0./configure make sudo make install git --version
Git completion 使用 git 命令的时候如果用 tab 没法补全则手动添加下 git-completion
1 2 curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o /etc/bash_completion.d/ source /etc/bash_completion.d/git-completion.bash
Question
fatal: unable to find remote helper for 'https'
1 ln -s /usr/lib/git-core/git-remote-https /bin/