运行前配置
配置用户信息
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
配置了email后github的仓库才会将提交记录统计到用户的贡献上。
查看配置信息
git config --list
初始化仓库
初始化一个新仓库
git init
克隆仓库
git clone https://github.com/xstoop/docker-dev.git
git clone https://github.com/xstoop/docker-dev.git docker #克隆仓库指定文件夹
文件操作
查看文件状态
git status
跟踪新文件或者暂存已修改的文件
git add filename
查看未暂存文件的更新/查看已修改状态的文件的修改
git diff [filename] # 指定文件名查看指定文件的更新,不加查看所有已修改文件的更新
查看已暂存文件的更新/查看已暂存文件与已提交的更新
git diff --staged/--cached [filename]
取消已经暂存的文件
git reset HEAD index.html
取消对文件的修改
git checkout index.html
提交更新
git commit
git commit -m 'Some text' # 提交更新并添加更新说明
git commit -a -m 'Some text' # 不进行暂存直接提交更新(只能提交已跟踪的文件)
git commit index.html index.html2 -m 'change something' # 提交指定已暂存的文件
git commit --amend # 修改最近一次的没有推送到远程仓库的提交(可修改提交信息、git add未暂存的文件)
复制指定的提交
使用git cherry-pick可以将一个分支的某一个提交复制到当前分支上
git cherry-pick ab134d0b9787cb40ea22b18835dbae27a27b0516
---------------------------------------------------------------
[develop 41fe8e35] test
Date: Mon Jun 29 11:50:58 2020 +0800
1 file changed, 1 insertion(+)
移除文件(取消跟踪文件并从工作目录删除文件)
git rm filename
取消跟踪文件(取消跟踪文件但不从工作目录删除)
git rm --cached filename
移动文件
git mv README.txt README
查看提交历史
git log
git log -p # 显示每次提交的内容差异
git log -2 # 显示最近的 n 条提交
git log --stat # 在底部显示简要的增改行数统计
git log --pretty=oneline|short|full|fuller # 指定使用完全不同于默认格式的方式展示
git log --oneline # --pretty=oneline --abbrev-commit 的简化用法,--abbrev-commit仅显示 SHA-1 的前几个字符,而非所有的 40 个字符
git log --since='2018-01-20' # 显示指定时间之后的提交
git log --until, --before # 显示指定时间之前的提交
git log --author # 显示指定作者相关的提交
git log -p --filepath # 某个文件或者路径的提交情况
显示文件中对每一行进行修改的最近一次提交
git blame file
版本回退与撤销提交
git reset 回退本地仓库,将HEAD指向历史中的某次提交,HEAD会往后移动。
git reset HEAD~n # 撤销最近n次提交 默认为--mixed
git reset --soft HEAD~1 # 回退1个版本, 且将这次提交的所有变更都移动到暂存区
git reset --mixed HEAD~1 # 回退1个版本,会重置暂存区,将这次提交的所有变更都移动到未暂存阶段
git reset --hard HEAD~1 # 回退1个版本,会重置暂存区,将这次提交的所有变更都重置到上次提交的版本
git revert 产生一个新的提交来撤销某次提交的修改,HEAD会往前移动。
git revert HEAD 撤销前一次 commit
git revert HEAD^ 撤销前前一次 commit
git revert {commit} 撤销指定的commit
当revert的commit是一个merge commit时,需要设置-m参数指定出保留哪条分支上的内容,因为merge合并时是将两个分支合并。使用git log可以查看合并的commit的两个父commit:
git log
---------------------------------------------------------------------------------------------------
commit 6ac7525a897529bf8c6cbb87ec41628f87421975 (origin/develop)
Merge: 1bdc2073 81de5e42 # 两个父commit
Author: xstoop <xstoop.xu@islide.cc>
Date: Mon Jun 29 10:29:24 2020 +0800
Merge branch 'feature/update_embed' into develop
....
-----------------------------------------------------------------------------
git revert -m 1 6ac7525a897529bf8c6cbb87ec41628f87421975 # 保留第一个父commit的内容
# git revert -m 2 6ac7525a897529bf8c6cbb87ec41628f87421975 保留第二个父commit的内容
-----------------------------------------------------------------------------
[develop 12798294] Revert "Merge branch 'feature/update_embed' into develop" # 产生一个新的提交
2 files changed, 8 deletions(-)
-----------------------------------------------------------------------------
注意:当使用了
git revert撤销一个merge commit并保留一个分支内容后,如果需要再次合并另一个分支的内容,直接合并只会得到另一个分支在使用git revert后提交的新的commit,因为之前的commit已经被撤销合并了。如果仍然需要全部合并,需要将使用git revert产生的那个commit给撤销掉,即需要撤销之前的撤销。
分支
新建分支
git branch testing
git checkout testing # 转换到新建的 testing 分支
git checkout -b testing # 从当前分支的最新提交对象处新建并转换到新建的 testing 分支
git checkout -b testing ee22afa9ad9f # 从当前分支之前的某一次提交对象处新建并转换到新建的 testing 分支
删除分支
git branch -d testing
合并分支
git checkout devel # 转换到devel 分支
git merge testing # 合并testing分支到devel 分支
git merge --squash testing # 合并时只会产生一个新的提交对象,不管被合并的分支上有多少次提交,这样会产生一个比较干净的提交历史
分支的变基(衍合)rebase
注意:变基操作请只在本地仓库使用,不要对已经推送到远程仓库的分支使用。
使用场景:
- 将多个历史提交对象合并成一个提交。让提交历史变得干净整洁。
- 更新已过时的分支。使用
git merge更新分支时会产生一个merge的提交对象。使用rebase则不会。
git rebase -i HEAD~4 # 合并最近4次提交
git rebase master # 将master分支的更新到当前分支
git rebase --edit-todo
git rebase --continue
查看分支
git branch # 当前所有本地分支
git branch -v # 查看各个分支最后一个提交对象的信息
git branch -r # 当前所有远程分支
git branch --merged # 查看哪些分支已被并入当前分支
git branch --no-merged # 查看尚未合并的分支
git branch -a # 查看所有远程分支与本地分支
推送分支
git push origin serverfix
跟踪远程分支
git checkout -b serverfix origin/serverfix
删除远程分支
git push origin :serverfix
储藏
新建储藏
git stash
git stash save 'message' # 添加说明
git stash list # 查看储藏
git stash apply stash@{0} # 应用储藏
git stash apply stash@{0} --index # 应用储藏被暂存起来的文件
git stash pop stash@{0} # 应用指定储藏并同时移除储藏
git stash drop stash@{0} # 删除指定储藏
git stash clear # 删除所有储藏
git stash show -p | git apply -R # 取消'应用储藏'
git stash branch textstashcreatebranch # 从储藏中创建分支(同时会删除储藏)
标签
git tag v1.2 # 新建标签
git tag -a v1.1 -m 'version 1.1' # 带注释的标签
git tag -a v1.0 2de7201a59cd32176a14664b8eb32a08f245448d # 指定提交对象新建标签
git tag # 查看所有标签
git show v1.2 --stat # 查看指定标签
git tag -l v1.1 # 搜索标签
git tag -d v1.0 # 删除标签
git push origin :refs/tags/v1.0 # 删除远程仓库的标签
git push origin v1.1 # 推送标签
git checkout v1.1 # 检出标签
远程仓库
git remote -v # 查看当前的远程仓库
git remote show [remote-name] # 查看某个远程仓库的详细信息
git remote add pb git://github.com/paulboone/ticgit.git # 添加远程仓库
git fetch [remote-name] # 从远程仓库查看更新,不自动合并到当前工作分支
git pull [remote-name] [branch] # 自动合并到本地仓库中当前分支
git push [remote-name] [branch-name] # 推送数据到远程仓库
git remote rename pb paul # 重命名远程仓库
git remote rm paul # 删除远程仓库
新建一个裸仓库的副本
git init --bare # 新建一个空仓库副本
git clone --bare my_project /opt/git/project.git # 从当前项目新建一个空仓库副本