git使用技巧记录

  • 取消被追踪文件
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
相关推荐
Simon—欧阳3 小时前
Git使用心得&理解
git
AI行业学习4 小时前
Claude Code + cc-switch + Git + Node.js 一站式完整安装配置教程【8.3】
git·python·安全·前端框架·node.js·html·notepad++
AI行业学习4 小时前
Claude Code + cc-switch + Git + Node.js 一站式完整安装配置教程(2026最新·国内可用版)
人工智能·git·python·安全·node.js·html·notepad++
维基框架15 小时前
OpenAI在给Git做优化 上游行为要变了
人工智能·git
1名持续学习的码农15 小时前
GPT Plus、GPT Pro用户第一次用Codex,项目权限和Git分支怎么设置?
人工智能·git·gpt·elasticsearch·ai编程·codex
晨曦中的暮雨1 天前
Learn Claude Code:CodeAgent 与后端程序大搭配——定时任务、Git Worktree 和MCP 服务
git·ai·llm·agent
减瓦1 天前
用 Git 为本地文件打造一台时光机
开发语言·git·编辑器
Huangjin007_1 天前
【Linux 系统篇(十一)】基础开发工具(六) —— 版本控制器 Git
linux·运维·git