Loading

Mac更新git,使用git创建仓库,PyCharm、Vscode中使用git上传项目

一、更新git

1.1 安装homebrew

Mac 更新git需要使用Homebrew, 而使用官方源下载较慢:/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
这里建议使用国内源。
运行:

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

然后按照提示选择相应的源安装即可。

1.2 更新git

安装最新版的 git:

brew install git

当看到下面输出时,说明 git 已经安装完成:

==> Summary
🍺  /usr/local/Cellar/git/2.20.1: 1,528 files, 41.3MB

1.3 卸载旧的git,然后指定新的git路径

cd /usr/local/bin/git
bash uninstall.sh
cd ~
open .bash_profile

修改.bash_profile:(这里"="两边不能有空格)

export GIT=/usr/local/Cellar/git/2.35.0
export PATH=$GIT/bin:$PATH

然后执行该文件:

source .bash_profile
git --version # 查看git版本信息是否正确
git config -l # 查看git配置信息

二、Vscode环境下使用git

2.1 github上新建一个repository

复制仓库SSH地址:
image
然后在项目主目录下:

git remote add origin git@github.com:Raiuny/git_work.git
git add .
git commit -m 'add xxx文件'
git push --set-upstream origin master # 指定仓库master文件夹作为上传目录
git push

如果本地没有公钥,这一步会报错:
image
也可以通过这一步来进一步验证上面的猜想:
image
从新生成新的密钥:

ssh-keygen -t rsa -C "youremailaddress" # 然后回车确认即可

image
打开生成的密钥:

cd ~/.ssh # 打开pub文件并复制里面的内容,后面要用

image
登录github,上传本地生成的公钥:
image
再次执行ssh -T git@github.com
image

2.2 git branch操作以及基本命令语句

git branch Branch1 # 本地新建一个分支
git checkout Branch1 # 切换到本地的某个分支
git push origin Branch1 # 将新分支发布在github上
git branch -d Branch1 # 在本地删除一个分支
git push origin :Branch1  # 在github远程端删除一个分支:(分支名前的冒号代表删除)

删除本地分支:git branch -d 分支名称
强制删除本地分支:git branch -D 分支名称, 
# 要想删除本地所在的分支,需要新建一个分支,然后切换到那个分支才能删除之前的分支。
删除远程分支:git push origin --delete 分支名称 
# 可用这个删除本地生成的master分支,然后使用main分支作为主分支

按照如下操作:
1.git init       //工作空间创建.git文件夹(默认隐藏了该文件夹)
2.git add .      //添加到暂存区
3.git commit -m "你的提交注释注释"
4.git remote add origin http://xxxxxxxxx.git   
//本地仓库和远程github关联<如果已经关联,运行这个会报fatal: remote origin already exists.说明远程链接已经存在> 只需要执行下面程序即可
5.git pull --rebase origin main  //远程有readme.md,拉一下
6.git push -u origin main        //代码合并,指定上传到的分支为main分支

2.3 使用Vscode自带的git操作按键

  • 初始化仓库
    image

点击初始化仓库相当于git init, 然后暂存更改后提交到远程

  • 暂存所有更改后点击✓提交到远程仓库。
    image
  • 点击云上传,发布分支远程建库
    image
  • 之后按照第二步暂存更改提交到远程仓库即可。

三、PyCharm环境下使用git

3.1 安装.ignore插件, 生成.gitignore文件

image

image

3.2 创建本地仓库

image

3.3 提交更改

image

3.4 远程建库

image

提交到远程仓库时候如果报错:
image

则需要重新设置git代理:

git config --global --unset http.proxy
git config --global --unset https.proxy
posted @ 2022-01-28 12:50  raiuny  阅读(1291)  评论(0编辑  收藏  举报