重大公告:2023年阿里云双11服务器限时大降价(1核2G1年仅需49元+2核4G1年100元) 速度点击查看!!!

Git实战经验技巧总结汇总(持续更新)

3,739次阅读
没有评论

学编程,学架构,拓视野,欢迎访问猿视野
本文永久更新地址:https://www.yundashi168.com/375.html
求学问道讲究:知其然,知其所以然。

git是目前世界上最先进的分布式版本控制系统。程序员必须掌握的技能之一。

教程资料

动画学习

这个动画学习网非常生动形象,还可以手写敲git命令,让你更牢记git命令并掌握git操作过程

精选资料

  • git-tips :trollface:Git的奇技淫巧
  • git-recipes Git recipes in Chinese by Zhongyi Tong. 高质量的Git中文教程.

常用命令

分支

创建分支

git checkout -b cloud-ipc-5.1.37 //创建本地分支cloud-ipc-5.1.37  并切换到该分支。
# git checkout -b 实际是两个git命令的合并。

git branch <branch-name>  # 创建分支,依然停留在当前的分支
# 注: <branch-name> 是分支的名称

切换分支

切换分支之前,原分支代码一定要记得commit.否者可能丢失代码。切记

git checkout branchName   //branchName 为分支名

查看所有分支

git branch -a //查看所以分支信息,并可以查看当前分支

查看当前分支

git branch  //查看当前分支和本地分支

查看远程分支

git branch -r  //查看远程分支信息

本地分支推送到远程分支

git push origin 远程分支名  //当前的本地分支推送到指定的远程分支上

git checkout -b feature-branch    //创建并切换到分支feature-branch  
git push origin feature-branch:feature-branch    //推送本地的feature-branch(冒号前面的)分支到远程origin的feature-branch(冒号后面的)分支(没有会自动创建)

检出远程分支到本地分支

git checkout -b feature-branch origin/feature-branch    //检出远程的feature-branch分支到本地

删除分支

git push origin --delete 远程分支名   //删除远程分支

// 删除本地分支
git branch -d localBranchName

// 删除远程分支
git push origin --delete remoteBranchName

刷新远程分支列表

git remote update origin --prune

常见问题

gitignore规则重新生效

gitignore重新生效

个人总结的gitignore忽略文件重新生效(亲测有效)


已经纳入git工作区的文件和目录,需要把.gitignore文件和【需要忽略的文件和文件目录】先移除git暂存区,否则修改的gitignore规则不会生效!

git rm -r --cached .
git add .
git commit -m "update .gitignore"

Git实战经验技巧总结汇总(持续更新)

git强制覆盖本地分支代码

【危险操作】放弃本地修改,使用服务器代码覆盖本地的Git命令如下:

1 $ git fetch --all
2 $ git reset --hard origin/master
# pull命令相当于fetch+合并代码命令
3 $ git pull

使用master分支覆盖本地。使用其它分支,则更改第二条命令的参数。

备注:

git fetch 只是下载远程的库的内容,不做任何的合并
git reset 把HEAD指向刚刚下载的最新的版本

git本地分支推送到远程分支

远程先开好分支然后拉到本地

git checkout -b feature-branch origin/feature-branch    //检出远程的feature-branch分支到本地

本地先开好分支然后推送到远程

$  git checkout -b feature-branch    //创建并切换到分支feature-branch  
$  git push origin feature-branch:feature-branch    //推送本地的feature-branch(冒号前面的)分支到远程origin的feature-branch(冒号后面的)分支(没有会自动创建)

git 修改远程命令

# url就是你的git地址
git remote set-url url

refusing to merge unrelated histories

解决方法:执行下面的git命令

git pull origin master --allow-unrelated-histories

问题产生的原因是:本地仓库与远程仓库不是一个仓库。

参考文献:

error: The following untracked working tree files would be overwritten by merge

解决方法:执行下面的git命令

git clean -d -fx

参考资料:

github强制用户提交代码不能用账户和密码的方式

提交代码报错:

Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information

报错信息:

$ git push github master
Logon failed, use ctrl+c to cancel basic credential prompt.
remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.
remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.
fatal: Authentication failed for 'https://github.com/Arisono/react-hooks-demo.git/'

解决方法:git remote set-url origin https://token@github.com/Arisono/react-hooks-demo.git/
教程参考:https://blog.csdn.net/weixin_41010198/article/details/119698015

报错原因:2021.8.13之后,github强制用户提交代码不能用账户和密码的方式。

思维导图

Git实战经验技巧总结汇总(持续更新)

资料文献

arison
版权声明:本站原创文章,由arison2021-09-06发表,共计6859字。
转载提示:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)