git 基础配置
git config --list --show-origin
用户信息
安装完 Git 之后,要做的第一件事就是设置你的用户名和邮件地址。因为每一个 Git 提交都会使用这些信息,它们会写入到你的每一次提交中
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
创建仓库
git init 初始化仓库
git remote -v 查看所有仓库地址
git remote add <name>(仓库别名) <remoteGitUrl>(仓库地址)
eg. git remote add origin xxx.git 添加源仓库
git remote remove <name>
版本提交
git status 查看工作区、暂存区的文件状态
git fetch <name> <branch> 拉取远程仓库版本,不自动合并
merge or rebase 拉取远程仓库版本并与本地副本自动合并
git add 暂存本地更改
git commit -m <message> 提交暂存更改至本地版本库
git push
打补丁
开发过程中,常常会出现这种情况:一个提交生成 commit
后,又发现了一个 bug
。若此时 commit
还没合入主干,此时可以使用 amend
,对上一个提交进行修改补充
git commit –amend 既可以对上次提交的内容进行修改,也可以修改提交说明。
等价于
git reset --soft HEAD^
git commit -c ORIG_HEAD
推送分支
git push 将本地版本分支推送至远程分支
git push <remote_repository_alias_name> <develope_branch>:<remote_branch> 推送本地至目标远程分支
eg. git push origin HEAD:refs/for/master 推送至 master 远程分支
git push origin master 推送至远程分支(如果已经关联远程上游分支)
git push --set-upstream <remote_repository_alias_name> <remote_branch_name> 发布本地分支至远程分支
切换分支
git branch 查看本地分支
git branch -a 查看所有分支(包含远程分支)
git checkout <branch> 切换分支
git branch -d <branch> 删除分支
git branch -D <branch> 强制删除分支
git branch -m <oldName> <newName> 修改分支名称
git checkout -b <branch> 创建并切换到本地分支
git checkout -b <develope_branch> <remote_name>/<remote_branch> 拉取远程分支并创建关联的本地分支
更新分支
当本地进行功能开发,本地无用分支太多,可以更新分支,与远程保持一致
git remote update <remote_name> --prune 更新分支
eg. git remote update origin --prune
分支合并
值得注意的是,merge
会产生额外的 commit
记录。为确保项目干净的 commit
记录,建议使用 rebase
工作流,而非 merge
git merge <branch> 合并分支(提交未合并)
git merge --squash <branch> 如有多次提交,合并为一次
变基
git rebase 改变一次提交记录的base
git rebase --continue 继续变基
git rebase --abort 终止变基
git rebase -i HEAD~<num> / git rebase -i HEAD^^ 变基 HEAD 前 N 个提交
git rebase -i --root 变基 HEAD 至第一个提交
git rebase -i <commit-id>
Git 工作流
主要分为两种方式
- merge
- rebase
对比两种方式,
rebase
工作流相比merge
工作流不会产生额外的merge
合并记录,提交记录更清晰
merge 工作流
- 开发前:切换开发分支 -> 具体功能开发
- 提交时:储藏本地更改 -> 拉取远程分支 -> 合并更改(生成合并记录) -> 弹出本地更改 -> 提交
git checkout <develop_branch>
// 本地功能开发...
git stash -m 'msg'
git fetch <repository> <branch>
git merge <repository>/<branch>
// git fetch + git merge = git pull
git stash apply stash@{0}
git add <changes>
git commit -m 'msg'
git push
rebase 工作流
- 开发前:切换开发分支 -> 具体功能开发
- 提交时:储藏本地更改 -> 拉取远程分支 -> 进行变基 -> 弹出本地更改 -> 提交
git checkout <develope_branch>
// 本地功能开发...
git stash -m <message>
git fetch <remote_name> <branch>
git rebase <remote_name>/<branch>
// git fetch + git rebase = git pull --rebase
git stash apply stash@{0}
git add <changes>
git commit -m <message>
git push
当本地分支落后远端多个提交时且存在冲突时,合并会把当前更改与远端存在的所有冲突进行手动合并
当本地分支落后远端多个提交时且存在冲突时,变基会使每个存在冲突的 commit
都需要进行手动变基
若本地分支只有你的更改,建议本地更改先储藏 stash
后,reset
到远端,再恢复你的暂存更改
版本信息、操作记录查询
查看版本信息、操作记录
git log 显示提交过的版本信息(不包括删除和reset的版本)
git reflog 显示该仓库本地进行的所有操作记录(包括删除、合并、变基和reset版本的操作)
版本回退
重置
git reset --hard <commit-id>
git reset --soft <commit-id>
恢复 / 反转
git revert <commit-id>
储藏
stash
操作不会创建版本信息,不会生成 commit-id
git stash -m <message>/ git stash save <message> 储藏你当前的工作区和暂存区的副本
git stash pop 恢复至最近的一次储藏(会删除 stash 记录)
git stash list 查看储藏列表
git stash apply <储藏号> 恢复至对应储藏版本(不会删除 stash 记录)
eg. git stash apply stash@{0}
合并任意分支 commit 到当前分支
git cherry-pick <commit-id1> <commit-id2> 挑选多个 commit-id 到当前分支
git cherry-pick --continue
git cherry-pick --abort
git cherry-pick --continue
git push
大小写不敏感的问题
使 git
对大小写敏感
git config core.ignorecase false