发布于 2024 年 10 月 21 日,星期一
这些技巧可能包括如何高效地管理分支、解决合并冲突、使用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 提交规范:
cmd 查看下载源
npm install -g nrm
nrm ls -> list
[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 c3d2e9a4e2a1e285ff4d8f06e01d4e3f19b532ea
$ 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 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 --soft 60a52b00d0ee2703156231e209e8aad115919aee // 暂存区$ git reset --hard 60a52b00d0ee2703156231e209e8aad115919aee // HEAD 指向此次提交记录$ git push origin HEAD --force // 强制推送远端
$ 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 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 a1b2c3d4e5f6a7b8c9d0e1f2g3h4i5j6k7l8m9n0
$ 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...
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
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 -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 checkout main
复制技术总监的代码到你的仓库中:
处理空白差异:
--ignore-all-space
选项来忽略所有的空白差异,进行一次合并。git add -Agit commit -m "Integrate technical director's latest changes"git diff --ignore-all-space
应用补丁忽略空白差异:
git apply --ignore-whitespace your_patch_file.patch
配置 Git 忽略空白差异:
git config --global apply.ignorewhitespace true
git branch --show-current
或者你也可以使用以下命令:
git rev-parse --abbrev-ref HEAD
# 1. 查看提交记录git log# 2. 回退到指定commit记录git reset --hard <commit_id># 3. 强制推送git push origin <branch_name> --force