Git本地与远程
前言
虽然用了github好久了,也学过一遍git但在项目实操的时候还是遇到好多问题,于是痛定思痛写下这篇文章结合实践中暴露出的毛病来重新学习学习Git,问题主要集中在远程仓库方面。
美化下git
工欲善其事必先利其器,磨刀不误砍柴工,配置下branch相关的颜色,更好康些,用起来也更舒服😁
git中输入
$ git config --global--edit
在里面加上这个👇
[color "branch"]
current = yellow reverse
local= green bold
remote = cyan ul
:wq保存退出后,看下效果吧
$ git branch -v
这样就把当前分支设置为好看的黄色,远端分支设置成青色,当地分支设为绿色o( ̄▽ ̄)d
你也可以按自己喜好来配置
以下是如何在 Git 配置文件中应用这些样式的示例:
[color "branch"]
current = yellow bold
local= green dim
remote = cyan ul
plain = white blink
upstream = magenta reverse
gone = red italic
颜色👇
normal:默认终端颜色
black:黑色文字
red:红色文字
green:绿色文字
yellow:黄色文字
blue:蓝色文字
magenta:洋红色文字
cyan:青色文字
white:白色文字
样式👇
bold:加粗
dim:暗淡
ul:下划线
blink:闪烁
reverse:反转
italic:斜体
将本地分支推到远程仓库
深入解析 git remote add origin
进入要上传的仓库,右键git bash,添加远程地址:
$ git remote add origin [email protected]:yourName/yourRepo.git
别小看这句经常使用的命令,里面可大有门道
之后常要用到origin,那它到底是什么?
可以通过👇查看
$ git remote -v
origin
是 Git 中的一个默认命名惯例,通常用于指代克隆的远程仓库。它是一个远程仓库的别名,用于简化和方便地引用和操作远程仓库。
1.默认远程仓库: 当你使用
git clone
命令克隆一个远程仓库时,Git 会自动将这个远程仓库命名为
origin
。例如:
git clone https://github.com/username/repository.git
这会创建一个名为
origin
的远程指针,指向克隆的仓库。
2.远程仓库的别名:
origin
是一个指向远程仓库 URL 的别名。你可以使用
origin
来代替实际的远程仓库 URL,简化命令操作。
也就是说我们其实可以将本地仓库和多个远程仓库建立联系
$ git remote add origin2 [email protected]:yourName/yourRepo.git
即将另一个远程仓库的ssh记为origin2
相关操作👇
添加新的远程仓库:
git remote add new-origin https://github.com/another-user/another-repository.git
修改现有远程仓库 URL:
git remote set-url origin https://github.com/username/new-repository.git
删除远程仓库:
git remote remove origin
深入解析git push
然后就是git正常操作了
gitadd.git commit -m"abab"git push origin main
真就这么简单吗?
nonono,too young too simple,必须得好好讲讲git push
git push
其实是一种缩写
如果你当前在
main分支上,并且该分支已经设置了仅有的远程分支,才可以直接使用
git push命令来推送本地的
main分支到远程仓库
所以当分支关联了多个远程仓库就不可以这么简单粗暴了
1.push到远程仓库的同名分支中(若无则建立)
git push <remote>
当然记得要
git checkout <branch>
切换到要推送的分支上
✨2.push到远程仓库的其他远程分支中
git push <remote><local-branch>:<remote-branch>
这个命令的语法是
git push <remote> <local-branch>:<remote-branch>。它的作用是将本地的
local-branch分支推送到远程仓库
remote的
remote-branch分支。
相关操作👇
删除远程分支:
git push <remote-name>--delete<branch-name>
补充下git branch相关命令
这个其是就是一些补充操作
git branch:查看本地分支git branch -r:查看远程分支git branch -a:查看所有分支(本地和远程)git branch -v:显示分支详细信息git branch <branch-name>:创建新分支git checkout -b <branch-name>:创建并切换到新分支git branch -d <branch-name>:删除本地分支git branch -D <branch-name>:强制删除本地分支git branch -m <new-branch-name>:重命名当前分支git branch -m <old-branch-name> <new-branch-name>:重命名指定分支git branch --set-upstream-to=<remote>/<branch> <local-branch>:设置上游分支
总结
只有熟练了
git branch
、
git remote
、
git push
三类指令才能玩转本地仓库/分支和远程仓库/分支
当然,在push or merge时候还会碰到个头疼问题,就是conflict冲突问题,有机会再说喽😁
版权归原作者 焦糖酒 所有, 如有侵权,请联系我们删除。