【git 本地管理版本及与github合并】 Init Push Pull操作解决方案

文章目录

使用环境: Ubuntu 20.04 & Git & GitHub


创建本地仓库,并与远程仓库链接

本地仓库:指在电脑硬盘中保存的项目文件夹
远程仓库:指在GitHub中保存的项目文件夹

首先创建一个新的空的文件夹:

bash 复制代码
mkdir YOUR_REPOSITORY_NAME # 创建自己的文件夹,可以任意命名

 
 
 
 
 
 
  
* 1

 

进入该文件夹,并且初始化git

bash 复制代码
cd YOUR_REPOSITORY_NAME # 根据具体情况输入你的文件夹路径,不要直接复制粘贴
git init # 初始化本地git,完成后文件夹内会出现一个.git文件,通过显示隐藏文件可以看到

 
 
 
 
 
 
  
* 1

  
* 2

 

初始化之后,如果你的远程仓库是早前已经创建好的,可以跳过这一步,如果当前你的项目没用对应的远程仓库,那么在GitHub创建一个repo即可

接下来,将本地仓库与远程仓库链接

bash 复制代码
git remote add origin 'your_url_name'

 
 
 
 
 
 
  
* 1

 

此处的 'your_url_name' 指的是你远程仓库的http地址,通过仓库主页的code按钮下拉菜单可以看到自己的url地址

上述命令运行之后,会要求你输入github的账号和密码,有时输入密码的部分会需要输入Personal Access Token,这部分的教程可以参考以下网址:

solving ERROR: password authentication was removed please use a personal access token instead:

bash 复制代码
https://stackoverflow.com/questions/68775869/support-for-password-authentication-was-removed-please-use-a-personal-access-to

 
 
 
 
 
 
  
* 1

 

after generating the Personal access tokens, save it and type it in the password blank when push & pull

完成后,此时本地仓库就和远程仓库相连了,我们就可以从terminal直接拉去远程仓库的代码到本地了,

但是在拉取之前,请确认是否有.gitignore文件

bash 复制代码
git pull origin master

 
 
 
 
 
 
  
* 1

 

如果报错: Fatal: Refusing to merge unrelated histories

那么说明你本地可能有一些其他的文件,和远程仓库对应不上,这时我们可以使用强制合并命令:

bash 复制代码
git pull origin master --allow-unrelated-histories

 
 
 
 
 
 
  
* 1

 

这样本地仓库就和远程仓库同步了

至此,配置部分结束

可以用以下命令来检查代码

bash 复制代码
git status # 查看本地仓库文件的跟踪状态

 
 
 
 
 
 
  
* 1

 

本节参考:https://www.datacamp.com/community/tutorials/git-push-pull


更新本地仓库并使用Push推送到远程仓库

1. 几种基础命令介绍:
bash 复制代码
git add . # 将本地文件全部添加到暂存区
# git add作用是将代码提交到暂存区
# git add .:将已经跟踪的进行了修改的文件以及新增加的文件提交到暂存区
# git add -u .:将跟踪的修改或删除的文件提交
# git add -a .:包括了前两项
# git add -i .:作用范围同-u
bash 复制代码
git commit -m 'message' # 将更新后的被追踪代码提交到本地branch中,相当于修改了本地仓库的状态

 
 
 
 
 
 
  
* 1

 
bash 复制代码
git push origin master # 将本地origin分支的代码推送到远程 master分支中

 
 
 
 
 
 
  
* 1

 
2. git push操作流程

当拥有本地仓库和远程仓库之后,可以通过git pull直接拉取远程仓库并合并到local branch,方法如下:

bash 复制代码
git pull repo_address branch_name

 
 
 
 
 
 
  
* 1

 

这里的repo_address指的是在Github clone http位置所显示的链接地址,branch_name指的是在remote repo中看到的branch的名字,通常是master,

所以一般如果拉取自己的仓库,命令就变成了:

bash 复制代码
# in a initialized dir ( after git init )
git pull https://github.com/XXX/XXX.git master

 
 
 
 
 
 
  
* 1

  
* 2

 
bash 复制代码
git status
git add
git commit -m 'message'
git push ORIGIN MASTER

ORIGIN is the branch that you have locally

MASTER is the branch on the remote repository


.gitignore


删除本地仓库,断开本地与远程的链接

step1 取消本地目录下关联的远程库:

bash 复制代码
git remote remove origin

 
 
 
 
 
 
  
* 1

 

step2 重新初始化本地仓库

bash 复制代码
git init
rm -rf .git # -rf means --recursive and --force

 
 
 
 
 
 
  
* 1

  
* 2

 

这样本地仓库就和远程仓库完全无关并且被清空了

设置用于提交commit的用户名,邮箱,以便github识别

  1. 查看自己现在的邮箱和用户名
bash 复制代码
git config -l
or 
git config --global user.name
git config --global user.email
  1. 更改自己的邮箱或用户名
    如果是新装的系统,git中没有自己的用户信息,同样可以用这个方法创建自己的用户名和邮箱
bash 复制代码
git config --global user.name "YOUR NAME"
git config --global user.email "YOUR EMAIL"

NOTE: 如果发现自己的commit没有在github的commit history中出现,并且repo的commit用户名点开不是自己,那就要考虑是否是上述两个参数没设置或者写错了

相关推荐
特种加菲猫2 小时前
构建完整工具链:GCC/G++ + Makefile + Git 自动化开发流程
linux·笔记·git·自动化
Franklin13 小时前
VS 版本更新git安全保护问题的解决
git
我是一只代码狗17 小时前
idea中合并git分支
git
我是一只代码狗17 小时前
idea中使用git
git·gitee·github
恋喵大鲤鱼17 小时前
git restore
git·git restore
李少兄18 小时前
Git Commit Message写错后如何修改?已Push的提交如何安全修复?
git·安全
Fireworkitte18 小时前
git stash
git
产品经理独孤虾19 小时前
GitHub Copilot:产品经理提升工作效率的AI助手
github·copilot·产品经理·原型设计·ai工具·效率提升·prd
杜莱20 小时前
IDEA 安装AI代码助手GitHub Copilot和简单使用体验
人工智能·github·intellij-idea