发布于 2024 年 10 月 21 日,星期一
Git使用技巧总结
这些技巧可能包括如何高效地管理分支、解决合并冲突、使用Git命令进行代码回滚、以及如何利用Git的强大功能来提高团队协作效率。通过总结这些技巧,博客旨在帮助前端开发者更好地掌握Git工具,提升代码管理和版本控制的能力,从而在项目开发中更加得心应手。
17.git 命令扩展:
/**
git branch -D <branch_name> 删除分支
将最新提交打标签 git tag v1.0
将指定commit打标签 git tag v0.9 4ab025
查看打标签 git tag
查看与某标签之间的差距 git show v0.9
添加远程分支
git remote add origin git@github.com:su37josephxia/hello-git.git
查看
git remote -v
本地新加一个新的远程库upstream git remote add upstream https://github.com/vuejs/vue-next
从远程仓库拉去代码 git fetch upstream
切换分支 git chekout master
合并远程分支 git merge upstream/master
撤销当前所有的更改
git restore
分支创建与合并
1. `git branch -a` 查看当前项目所有的分支
2. `git checkout branchName` 切换分支
3. `git checkout -b branchName` 创建并切换分支
4. `git rebase master` 讲master上的更改更新到当前分支
5. `git branch -d branchName` 删除分支
6. `git merge branchName` 合并分支
查看当前git用户邮箱
git config user.email
*/
18.npm 切换镜像:
-
查看镜像:
npm get registry
-
设置淘宝镜像命令:
npm config set registry http://registry.npm.taobao.org/
-
原始镜像:
https://registry.npmjs.org/
-
切换原始镜像:
npm config set registry https://registry.npmjs.org/
-
淘宝镜像:
http://registry.npm.taobao.org/
-
切换到官方淘宝镜像:
npm config set registry http://www.npmjs.org
-
nrm
切换镜像源 -
npm config list
查看 npm 的镜像源 -
查看一下当前源 yarn config get registry
-
切换为淘宝源 yarn config set registry https://registry.npm.taobao.orghttps://registry.npm.taobao.org
-
或者切换为自带的 yarn config set registry https://registry.yarnpkg.comhttps://registry.yarnpkg.com
-
全局安装:
npm install -g nrm
-
查看可切换的镜像源:
nrm ls
(* 表示正在使用的镜像源)
* npm -------- https://registry.npmjs.org/ yarn ------- https://registry.yarnpkg.com/ cnpm ------- http://r.cnpmjs.org/ taobao ----- https://registry.npm.taobao.org/ nj --------- https://registry.nodejitsu.com/ npmMirror -- https://skimdb.npmjs.com/registry/ edunpm ----- http://registry.enpmjs.org/
- 切换淘宝镜像源:
nrm use taobao
(7) 全局删除,rimraf
- 下载:
cnpm install -g rimraf
- 删除文件:
rimraf xxxx
31.git 提交规范:
- type:commit 类型
- feat:新功能、新特性
- fix:修改 bug
- perf:更改代码,以提高性能
- refactor:代码重构(重构,在不影响代码内部行为,功能下的代码修改)
- docs:文档修改
- style:代码格式修改,注意不是 css 修改(例如分号修改)
- test:测试用例新增、修改
- build:影响项目构建或依赖项修改
- revert:恢复上一次提交 ci:持续集成相关文件修改
- chore:其他文件修改(不在上述类型中的修改)
- release:发布新版本
- workflow:工作流相关文件修改
- 1.scope:commit 影响的范围:比如:route,component,utils,build...
- 2.subject:commit 的概述
- 3.body:commit 具体修改内容,可以分为多行
- 4.footer:一些备注,通常是 breaking change 或修改的 bug 的链接
cmd 查看下载源
npm install -g nrm
nrm ls -> list
git 如何将同一个仓库同时提交到 github 和 gitee 上?
- 在 github 上新建一个仓库
- 在 gitee 上将 github 的仓库导入,此时需要管分支,因为是从 github 上导入的仓库,此时只有一个仓库
- 打开 .git 文件夹下的 config 文件,按照以下进行配置
[core] repositoryformatversion = 0 filemode = false bare = false logallrefupdates = true symlinks = false ignorecase = true[remote "origin"] # Github url = git@github.com:yaolifeng0629/Foreign-advanced-level.git # Gitee url = https://gitee.com/yaolifeng0529/Foreign-advanced-level.git fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main
如何撤销最近的已经合并的某次提交?
- 查看提交历史,找到最新的合并提交
$ git logcommit c3d2e9a4e2a1e285ff4d8f06e01d4e3f19b532ea (HEAD -> master)Author: Hanmeimei <hanmeimei@example.com>Date: Fri Jun 30 15:26:43 2023 +0800 Incorrect merge commitcommit 3e5fb8a7b631eb6492ef32e28a813084d4d3de2bAuthor: Lilei <Lilei@example.com>Date: Thu Jun 29 18:20:56 2023 +0800 Correct commit...
- 使用 git revert 命令撤销合并提交,Git 将自动创建一个新的提交,撤销错误的合并提交。
$ git revert c3d2e9a4e2a1e285ff4d8f06e01d4e3f19b532ea
- 使用 git log 或 git show 命令验证新的提交历史,确认错误的变更已经被撤销。
$ git logcommit b254d0f063b4ab4e7b78fb42015e0c55e0e98712 (HEAD -> master)Author: Hanmeimei <hanmeimei@example.com>Date: Fri Jun 30 15:46:28 2023 +0800 Revert "Incorrect merge commit" This reverts commit c3d2e9a4e2a1e285ff4d8f06e01d4e3f19b532ea.commit 3e5fb8a7b631eb6492ef32e28a813084d4d3de2bAuthor: Lilei <lilei@example.com>Date: Thu Jun 29 18:20:56 2023 +0800 Correct commit...
如何撤销最近的已经合并的多次提交?
- 首先使用 git log 命令查看提交历史,找到要回滚的哈希值。
$ git logcommit c5b890eee2edf9a353ec6bba0543e41d2529a8f8 (HEAD -> master)Author: Hanmeimei <hanmeimei@example.com>Date: Mon Jul 3 15:12:10 2023 +0800 Incorrect merge commitcommit 82bcf43083a4dc8c87091ebde4dd5374f0c6e274Author: Hanmeimei <hanmeimei@example.com>Date: Mon Jul 3 15:11:54 2023 +0800 Incorrect merge commit2commit 60a52b00d0ee2703156231e209e8aad115919aeeAuthor: Hanmeimei <hanmeimei@example.com>Date: Mon Jun 26 06:32:35 2023 +0000 Correct commit...
- 使用 git reset 命令撤销合并提交,并创建一个新的提交来还原到正确的状态。
$ git reset --soft 60a52b00d0ee2703156231e209e8aad115919aee // 暂存区$ git reset --hard 60a52b00d0ee2703156231e209e8aad115919aee // HEAD 指向此次提交记录$ git push origin HEAD --force // 强制推送远端
- 注意:此次提交之后的修改不做任何保留,git status 查看工作区是没有记录的。
- 最后,使用 git log 或 git show 命令验证提交历史,确认错误的变更已经被撤销。
$ git logcommit 60a52b00d0ee2703156231e209e8aad115919aee (HEAD -> master)Author: Hanmeimei <hanmeimei@example.com>Date: Mon Jun 26 06:32:35 2023 +0000 Correct commit...
- 如果出现了误删,可以用以下办法来恢复:
$ git reset --hard 82bcf43083a4dc8c87091ebde4dd5374f0c6e274 // 误删的哈希值 HEAD is now at 82bcf4308 feat: Incorrect merge commit2$ git push origin HEAD --force // 强制推送远端
如何撤销中间合并某一个提交?
- 首先使用 git log 命令查看提交历史,并找到要撤销的中间合并提交。
$ git logcommit 3e5fb8a7b631eb6492ef32e28a813084d4d3de2b (HEAD -> master)Author: Lilei <lilei@example.com>Date: Wed Jun 21 12:00:00 2023 +0000 Correct commitcommit a1b2c3d4e5f6a7b8c9d0e1f2g3h4i5j6k7l8m9n0Author: Hanmeimei <hanmeimei@example.com>Date: Fri Jun 18 12:00:00 2023 +0000 Incorrect merge commit...
- 然后使用 git revert 命令撤销合并提交,并创建一个新的提交来还原到正确的状态。
$ git revert a1b2c3d4e5f6a7b8c9d0e1f2g3h4i5j6k7l8m9n0
- 最后使用 git log 或 git show 命令验证新的提交历史,确认错误的变更已经被撤销。
$ git logcommit b254d0f063b4ab4e7b78fb42015e0c55e0e98712 (HEAD -> master)Author: Hanmeimei <hanmeimei@example.com>Date: Mon Jun 28 12:10:00 2023 +0000 Revert "Incorrect merge commit" This reverts commit a1b2c3d4e5f6a7b8c9d0e1f2g3h4i5j6k7l8m9n0.commit 3e5fb8a7b631eb6492ef32e28a813084d4d3de2bAuthor: Lilei <lilei@example.com>Date: Wed Jun 21 12:00:00 2023 +0000 Correct commitcommit a1b2c3d4e5f6a7b8c9d0e1f2g3h4i5j6k7l8m9n0Author: Hanmeimei <hanmeimei@example.com>Date: Fri Jun 18 12:00:00 2023 +0000 Incorrect merge commit...
- 可以看到之前的提交仍会保留在 git log 中,而此次撤销会做为一次新的提交(Revert "Incorrect merge commit")。
如何切换到别人的 pr 分支修改东西?
- 确保本地是干净的,可以先用 git stash 存起来,等你改完之后再 git stash pop 出来
- 命令式:
1. git checkout pr/[userName]/[pr id] 比如 git checkout pr/liudewa888/2602. 然后就是正常的 git add & git commit3. push时: git push [userName] HEAD:对应的分支名称 比如 git push Wecle HEAD:feat/locate-the-current-course-card
- 可视化操作:
- vscode 下载: GitHub Pull Requests 插件
如何在 pr 分支拉取最上游的最新代码?
git pull origin main# orgit pull origin upstream
git fetch upstream
: 这个命令用于从你 fork 的原始仓库(通常称为 upstream)获取最新的历史记录和分支信息,但不立即合并到你的本地分支。git pull origin main
: 这个命令用于从你自己的远程仓库(通常是 origin)获取最新的历史记录和分支信息,并且立即合并到你的当前本地分支。
如何还原本地修改或添加的某个文件?
- 如何只删除某个文件呢?
git clean -f -e '!'untracked_file.txt
- 因为以上命令会永久删除文件,如何想进一步确定,可以执行```bashgit clean -n -e '!'untracked_file.txt```
- 如何删除全部未跟踪的文件或目录?
- 运行 git clean 命令, 以确保列出的文件是你确实想要删除的:
git clean -n
- 以确保列出的文件是你确实想要删除的:
# 只删除文件git clean -f# 删除文件和目录git clean -fd
克隆超时如何解决?
Failed to connect to github.com port 443 : Timed out
解决方法:
1.本地开启一个“学术网络”代理
2.配置 git 代理
注意修改成自己的 IP 和端口号git config --global http.proxy http://127.0.0.1:7890git config --global https.proxy http://127.0.0.1:7890
如果想取消代理,执行以下命令:
取消代理git config --global --unset http.proxygit config --global --unset https.proxy
查看代理
git config --global --get http.proxygit config --global --get https.proxy
git 如何忽略空白差异?
-
切换到正确的分支:
- 确保你在你的新建仓库的主分支上,准备合并技术总监的代码。
git checkout main
-
复制技术总监的代码到你的仓库中:
- 将技术总监的代码直接复制到你的项目目录中,覆盖掉现有的文件。
-
处理空白差异:
- 使用 Git 的
--ignore-all-space
选项来忽略所有的空白差异,进行一次合并。
git add -Agit commit -m "Integrate technical director's latest changes"git diff --ignore-all-space
- 使用 Git 的
-
应用补丁忽略空白差异:
- 如果你有一个补丁文件,可以使用以下命令来应用补丁,并忽略所有空白差异。
git apply --ignore-whitespace your_patch_file.patch
-
配置 Git 忽略空白差异:
- 配置 Git 忽略所有空白差异,可以全局设置,也可以在本地仓库设置。
git config --global apply.ignorewhitespace true
git 如何显示当前所在的分支名?
git branch --show-current
或者你也可以使用以下命令:
git rev-parse --abbrev-ref HEAD
线上代码库如何恢复到某个commit记录?
# 1. 查看提交记录git log# 2. 回退到指定commit记录git reset --hard <commit_id># 3. 强制推送git push origin <branch_name> --force