介绍
先要下载git软件Git for Windows
之后我们便能得到俩个命令
1.新建一个文件夹
2.通过命令git init把这个文件夹变成Git可管理的仓库
在你的新文件夹内中通过 Open Git Bash here打开命令窗口,在窗口输入git init将文件夹变成可管理仓库,会多出一个.git文件夹,记得要把隐藏关了
3.这时候你就可以把你的项目粘贴到这个本地Git仓库里面(粘贴后你可以通过git status来查看你当前的状态),然后通过git add把项目添加到仓库(或git add .把该目录下的所有文件添加到仓库,注意点是用空格隔开的)。在这个过程中你其实可以一直使用git status来查看你当前的状态。
首先检查一下文件状态:
git status
如果有修改过的文件,使用以下命令将它们添加到 Git 缓存区:
git add .
然后进行一次提交:
git commit -m "Initial commit"
4.创建SSH KEY。先看一下你C盘用户目录下有没有.ssh目录,有的话看下里面有没有id_rsa和id_rsa.pub这两个文件,有就跳到下一步,没有就通过下面命令创建
ssh-keygen -t rsa -C "youremail@example.com"
5.登录Github,找到右上角的图标,打开点进里面的Settings,再选中里面的SSH and GPG KEYS,点击右上角的New SSH key,然后Title里面随便填,再把刚才id_rsa.pub里面的内容复制到Title下面的Key内容框里面,最后点击Add SSH key,这样就完成了SSH Key的加密。具体步骤也可看下面:
把id_rsa.pub里的内容复制到key里
6.在Github上创建一个Git仓库,你可以直接点New repository来创建。
7.在Github上创建好Git仓库之后我们就可以和本地仓库进行关联了,根据创建好的Git仓库页面的提示,可以在本地仓库的命令行输入:
关联仓库的HTTPS
git remote add origin https://github.com/Demonskirito/csdn-picui.git
推送仓库
git push -u origin master //空仓
git push origin master /非空
如果报以下错,可以试试ssh的推送
29199@DESKTOP-OL7DIJF MINGW64 ~/Desktop/csdn-picui (master)
$ git push origin master
fatal: unable to access 'https://github.com/Demonskirito/csdn-picui.git/': Failed to connect to 127.0.0.1 port 7897 after 2049 ms: Couldn't connect to server
修改 Git 仓库的远程 URL 为 SSH
git remote set-url origin git@github.com:Demonskirito/csdn-picui.git
git push origin master
推送的时候不用梯子
尽量别太大,我这超了,爆红了
使用git对github操作的详细使用步骤
根据上文先绑定远端仓库,为了不多开分支,我们要拉取远端的默认分支,不然会新开一个master分支,我接受不了.....
每次都会多一个分支出来贼难受,而且在提交时会报错,需要处理,具体报错如下:
29199@DESKTOP-OL7DIJF MINGW64 ~/Desktop/shopping-platform (master) $ git add . warning: in the working copy of 'pom.xml', LF will be replaced by CRLF the next time Git touches it warning: in the working copy of 'src/main/java/com/example/shopping/ShoppingApplication.java', LF will be replaced by CRLF the next time Git touches it warning: in the working copy of 'src/main/resources/mapper/CarMapper.xml', LF will be replaced by CRLF the next time Git touches it warning: in the working copy of 'src/test/java/com/example/shopping/ShoppingApplicationTests.java', LF will be replaced by CRLF the next time Git touches it warning: in the working copy of 'target/classes/mapper/CarMapper.xml', LF will be replaced by CRLF the next time Git touches it
这些警告是因为 Git 检测到文件的行尾符号(Line Endings)在本地和仓库的标准之间存在差异。主要是因为 Windows 使用 CRLF
(回车+换行)作为行尾,而 Unix/Linux 和 Git 通常使用 LF
(换行)。
全局设置(推荐)
git config --global core.autocrlf true
为了不多创建分支
//拉取远程分支
git fetch origin main
//切换到 main 分支
git checkout -b main origin/main
// 验证切换是否成功
git branch
//输出应该是
* main
这样就可以在这个分支下进行操作了
如果已经创建了master分支,我们也可以将这两个分支合并
//将 master 合并到 main(可选)
git merge master
//如提示 unrelated histories 错误,则使用:
git merge master --allow-unrelated-histories
//推送本地 main 到远程
git push origin main
这样就算操作成功了。