- 取消被追踪文件
bash
git rm -r -n --cached 文件、目录 // 递归列出将要取消追踪的文件
bash
git rm -r --cached 文件、目录 // 递归取消文件的追踪
- 撤销未提交的变更
bash
git clean -n // 列出可以被删除的未追踪的文件
bash
git clean -f // 删除未追踪的文件
bash
git clean -df // 清理未追踪的文件和文件夹
- 撤销 add
bash
git reset HEAD filename
- 推送本地分支到远端
bash
git checkout -b 3.0.1
bash
git push --set-upstream origin 3.0.1
- 拉取远程分支到本地
bash
git checkout -b 本地分支名x origin/远程分支名x
bash
git fetch origin 远程分支名x:本地分支名x
- 撤销 commit,修改的内容还在暂存区
bash
git log // 查看节点的 commit id
bash
git reset commit_id
bash
git commit --amend // 修改上次提交但未 push 的 commit message
- 撤销未 push 的 commit,修改的内容直接消失
bash
git reset --hard commit_id
- 撤销 push 的内容
// 还原已经提交的修改,此次操作之前和之后的 commit 和 history 都会保留,并且把这次撤销作为一次最新的提交
bash
git revert HEAD // 撤销前一次 commit
bash
git revert HEAD^ // 撤销前前一次 commit
bash
git revert commit-id // 撤销指定的版本,撤销也会作为一次提交进行保存
git revert 是提交一个新的版本,将需要 revert 的版本的内容再反向修改回去,版本会递增,不影响之前提交的内容。
// 对于合并的时候会产生一个 merge commit,等于说有两个 parent commit_id,对于这样的撤销
bash
git show commit_id // 查看合并的那个提交
// 如下所示,合并的会有两个 parent commit,代表是从哪两个分支合并过来的
bash
// git show bd86846
// commit bd868465569400a6b9408050643e5949e8f2b8f5
// Merge: ba25a9d 1c7036f 第一个编号为 1
bash
git revert -m 1 bd86846 // 撤销合并 bd86846,保留编号为 1 的分支,剔除编号为 2 的分支
- Git 删除分支
bash
本地分支:git branch -d branchname
bash
远程分支:git push origin --delete branchname
- 查看本地和远程的所有分支和 tag 情况:
bash
git ls-remote
- Git 全局设置
bash
git config --global user.name "lj"
bash
git config --global user.email "liujian_haha@163.com"
- 创建新仓库
bash
git clone http://gitlab.yangpijuan.club:9999/ios/ypj.git
bash
cd ypj
bash
touch README.md
bash
git add README.md
bash
git commit -m "add README"
bash
git push -u origin master
- push 一个已经存在的文件夹到仓库
bash
cd existing_folder
bash
git init
bash
git remote add origin http://gitlab.yangpijuan.club:9999/ios/ypj.git
bash
git add .
bash
git commit -m "Initial commit"
bash
git push -u origin master
- push 一个已经存在的仓库
bash
cd existing_repo
bash
git remote rename origin old-origin
bash
git remote add origin http://gitlab.yangpijuan.club:9999/ios/ypj.git
bash
git push -u origin --all
bash
git push -u origin --tags
- 迁移旧仓库到新仓库,需要在旧仓库目录下,新仓库需要先有分支(如 master)
bash
git remote rename origin old-origin // 备份原来的仓库路径到 old-origin
bash
git remote add origin https://gitee.com/jouypub/json.git // 添加新的仓库地址到 config 文件
// origin 指定现在的远端分支,allow-unrelated-histories 表示允许同步不相关的历史,不加这个同步会报错:fatal: refusing to merge unrelated histories
bash
git pull origin master --allow-unrelated-histories
bash
git push // 推送新的内容到远端
- 忽略文件不生效
// .gitignore 需要放在根目录下和 .git 同目录下,如果没有,手动创建就可以
// .gitignore 只能忽略未被追踪的文件,如果文件已经被加入版本控制,则修改忽略文件是无效的
// 解决办法:可以先把本地缓存删除,然后再重新提交
bash
git rm -r --cached .
bash
git add .
bash
git commit -m 'update .gitignore'
- 修改 git 提示语言
// 资料来源:https://askubuntu.com/questions/320661/change-gits-language-to-english-without-changing-the-locale
bash
echo "export LANGUAGE='en_US git'" >> ~/.bashrc // 改成英文
bash
echo "export LANGUAGE='en_GB git'" >> ~/.bashrc // 改成中文
- tag
bash
git tag // 列出所有 tag
bash
git tag -a 2.0.1 -m "2.0.1版本打包" // 创建本地 tag
bash
git show 2.0.1 // 显示 2.0.1 tag 的信息
bash
git push origin 2.0.1 // 推送 tag 到远端
bash
git push origin --tags // 推送所有 tag 到远端
bash
git tag -d 2.0.1 // 删除 2.0.1 本地分支
bash
git push origin :refs/tags/2.0.1 // 删除远端 2.0.1 分支
bash
git push origin --delete 2.0.1 // 删除远端 2.0.1 分支
- stash
bash
git stash save "save message" // 暂存 stash,也可不加 save 参数
bash
git stash list // 列出 stash 的列表
bash
git stash pop // 恢复 stash 的内容
bash
git stash clear // 删除所有的 stash
- 更换 git 服务器路径,tag,branch,log 保留
bash
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done // 下载所有分支到本地
bash
git remote -v // 查看所有的源
bash
git remote rename origin old-origin // 保留原来的源
bash
git remote add origin http://url // url 为新服务器仓库所在地址
bash
git push origin --all // 上传所有的 branch
bash
git push origin --tag // 上传所有 tag
bash
git remote rm old-origin // 删除不要的源
// 新的服务器上的代码上传完毕
bash
git push --set-upstream origin master // 设置本地分支追踪远端 master
- 绑定本地分支到远端,推送本地分支到远端
bash
git push --set-upstream origin remote // 当前本地分支推送到远端 remote
bash
git branch --set-upstream-to=origin/remote local // 绑定本地分支 local 到远端分支 remote
- 撤销/回退
// 使用 reset --hard 之后两个 logID 之间的数据和日志会全部被删除
bash
git reset --hard/soft/mixed logID // 从本地仓库撤销到编辑前(删除)、从本地仓库撤销到暂存区(add 后,commit 前)、从本地仓库撤销到工作区(编辑后,add 前)
// 使用 reset 相当于重新编辑以前的某个 logID,中间的日志不会被清除
bash
git revert logID
- 查看合并树
bash
git log --oneline --graph --decorate --all
- 各个分支之间的关系
bash
git log --graph --decorate --oneline --simplify-by-decoration --all
- log/show
bash
git log --name-status // 每次修改的文件列表,显示状态
bash
git log --name-only // 每次修改的文件列表
bash
git log --stat // 每次修改的文件列表,及文件修改的统计
bash
git whatchanged // 每次修改的文件列表
bash
git whatchanged --stat // 每次修改的文件列表,及文件修改的统计
bash
git show // 显示最后一次的文件改变的具体内容
bash
git show -5 // 显示最后 5 次的文件改变的具体内容
bash
git show commitid // 显示某个 commitid 改变的具体内容
- 合并 A 分支的某次提交到 B 分支
bash
git switch A // 切到 A 分支
bash
git pull // 拉取最新代码
bash
git log // 查询需要合并的那次提交的 commit_id
bash
git switch B
bash
git pull
bash
git cherry-pick commit_id // 根据 id 查询指定的内容
bash
git pull // 拉取
bash
git push
- 合并 A 分支的一系列提交(commitID1 ~ commitID2)到 B 分支
bash
git switch A // 切到 A 分支
bash
git pull
// 在 A 的基础上新建 newA 分支,指定最后一次提交
bash
git checkout -b newA commitID2