git
Git
一、git常用命令
命令名称 | 作用 |
---|---|
git config --global user.name 用户名 | 设置用户签名 |
git config --global user.email 邮箱 | 设置用户email地址 |
git init | 初始化本地库 |
git status | 查看本地库状态 |
git add 文件名 | 添加到暂存区 |
git commit -m “日志信息” 文件名 | 提交到本地库 |
git reflog | 查看历史记录 |
git reset --hard 版本号 | 版本穿梭 |
设置用户签名
git config --global user.name 用户名
git config --global user.email 邮箱
说明:签名的作用是区分不同操作者身份。用户的签名信息在每一个版本的提交信息中能够看到,以此确认本次提交是谁做的。 Git 首次安装必须设置一下用户签名,否则无法提交代码。
注意: 这里设置用户签名和将来登录 GitHub(或其他代码托管中心)的账号没有任何关系。
初始化本地库
基本语法:git init
生成一个空的git库
该文件夹是一个隐藏的文件夹,这里我们需要用ll -a来查看这个隐藏文件
实例:
# 首次查看(工作区没有任何文件)
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
# 新增文件(hello.txt)
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ vim hello.txt
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ cat hello.txt
cat: hello.txt: No such file or directory
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ ls
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ vim hello.txt
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ cat hello.txt
hi
# 再次查看(检测到未追踪的文件)
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.txt
nothing added to commit but untracked files present (use "git add" to track)
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
添加暂存区
基本语法:git add 文件名
案例实操:
此时由红色变为绿色,表示已经添加到了暂存区
只是删除掉暂存区的文件内容:
提交本地库
基本语法:git commit -m "日志信息" 文件名
案例实操:
查看历史
这里reflog只是一个版本好的前七位精简版本,完整版的使用log 命令获得
修改文件
#编辑文档
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ vim hello.txt
#再次提交
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git add hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: hello.txt
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git commit -m "second commit" hello.txt
[master fee56d9] second commit
1 file changed, 1 insertion(+), 1 deletion(-)
#这里是只能一行删除,一行插入
版本穿梭
#当前处于第二个版本
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git reflog
fee56d9 (HEAD -> master) HEAD@{0}: commit: second commit
ab2989e HEAD@{1}: commit (initial): first commit
#再次编辑
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ vim hello.txt
#进行第三次提交
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git commit -m "third commit" hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
[master 103a51d] third commit
1 file changed, 1 insertion(+)
#再次打印 已经到了第三次提交了
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git reflog
103a51d (HEAD -> master) HEAD@{0}: commit: third commit
fee56d9 HEAD@{1}: commit: second commit
ab2989e HEAD@{2}: commit (initial): first commit
#现在在第三个版本
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git reflog
103a51d (HEAD -> master) HEAD@{0}: commit: third commit
fee56d9 HEAD@{1}: commit: second commit
ab2989e HEAD@{2}: commit (initial): first commit
#版本穿梭回第二个版本
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git reset --hard fee56d9
HEAD is now at fee56d9 second commit
#reset 回去
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git reflog
fee56d9 (HEAD -> master) HEAD@{0}: reset: moving to fee56d9
103a51d HEAD@{1}: commit: third commit
fee56d9 (HEAD -> master) HEAD@{2}: commit: second commit
ab2989e HEAD@{3}: commit (initial): first commit
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ cat hello.txt
hikkkiiii222222222
二、git分支操作
分支的好处
同时并行推进多个功能开发,提高开发效率。
各个分支在开发过程中,如果某一个分支开发失败,不会对其他分支有任何影响。失败的分支删除重新开始即可。
分支的操作
命令名称 | 作用 |
---|---|
git branch 分支名 | 创建分支 |
git branch -v | 查看分支 |
git checkout 分支名 | 切换分支 |
git merge 分支名 | 把指定的分支合并到当前分支上 |
查看分支
基本语法:git branch -v
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git branch -v
* master fee56d9 second commit
创建分支
基本语法:git branch 分支名
#创建一个名为 hot-fix的分支
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git branch hot-fix
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git branch -v
hot-fix fee56d9 second commit
* master fee56d9 second commit
刚创建的新的分支,并将主分支master的内容复制了一份。
切换分支
基本语法:git checkout 分支名
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'
#当前所在是hot-fix分支
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (hot-fix)
$
再次查看
切换分支后,在新分支修改文件:
#重新编辑hello.txt
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (hot-fix)
$ vim hello.txt
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (hot-fix)
$ git status
On branch hot-fix
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: hello.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
mintty.2023-01-11_16-40-13.png
no changes added to commit (use "git add" and/or "git commit -a")
#修改之后提交
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (hot-fix)
$ git add hello.txt
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (hot-fix)
$ git status
On branch hot-fix
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: hello.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
mintty.2023-01-11_16-40-13.png
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (hot-fix)
$ git commit -m "hot-fix firstcommit" hello.txt
[hot-fix 88aa35e] hot-fix firstcommit
1 file changed, 1 insertion(+)
合并分支(正常合并)
基本语法:git merge 分支名
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (hot-fix)
$ git checkout master
Switched to branch 'master'
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ cat hello.txt
hikkkiiii222222222
#将hot-fix 合并到master分支上
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git merge hot-fix
Updating fee56d9..88aa35e
Fast-forward
hello.txt | 1 +
1 file changed, 1 insertion(+)
合并分支(冲突合并)
冲突产生的原因
并分支时,两个分支在同一个文件的同一个位置有两套完全不同的修改。 Git 无法替我们决定使用哪一个,因此,必须人为决定新代码内容。
产生冲突
首先,在master修改文件hello.txt最后一行内容,并提交:
#在master修改 hello.txt,提交
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ vim hello.txt
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git add hello.txt
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git commit -m "master test" hello.txt
[master 53ee714] master test
1 file changed, 1 insertion(+), 1 deletion(-)
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ cat hello.txt
hot-fix 111
hikkkiiii222222222 master test
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'
#在hot-fix修改 hello.txt,提交
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (hot-fix)
$ vim hello.txt
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (hot-fix)
$ git add hello.txt
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (hot-fix)
$ git commit -m "hot-fix test" hello.txt
[hot-fix 9fd165a] hot-fix test
1 file changed, 1 insertion(+), 1 deletion(-)
#切换回master分支
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (hot-fix)
$ git checkout master
Switched to branch 'master'
#去合并
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git merge hot-fix
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.
#再次查看状态
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master|MERGING)
$ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: hello.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
mintty.2023-01-11_16-40-13.png
no changes added to commit (use "git add" and/or "git commit -a")
#手动编辑合并
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master|MERGING)
$ vim hello.txt
解决冲突
编辑有冲突的文件,删除特殊符号,决定要使用的内容
特殊符号:
<<<<<<< HEAD
当前分支的代码
=======
合并过来的代码
>>>>>>> hot-fix
修改的时候这个地方会产生<<< Head等符号,需要这里进行这些特殊字符。
#添加到暂存区
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master|MERGING)
$ git add hello.txt
#提交的时候不可以带文件名
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master|MERGING)
$ git commit -m "merge test" hello.txt
fatal: cannot do a partial commit during a merge.
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master|MERGING)
$ git commit -m "merge test"
[master 067d86e] merge test
#这里只会修改你合并了的分支,不会修改你没有合并过的分支
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'
#再次查看hot-fix分支的内容,发现这里并没有修改
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (hot-fix)
$ cat hello.txt
hot-fix 111 hot-fix test
hikkkiiii222222222
分支的切换归根到底是指针的切换
master、 hot-fix 其实都是指向具体版本记录的指针。当前所在的分支,其实是由 HEAD决定的。所以创建分支的本质就是多创建一个指针。
HEAD 如果指向 master,那么我们现在就在 master 分支上。
HEAD 如果执行 hot-fix,那么我们现在就在 hot-fix 分支上。
所以切换分支的本质就是移动 HEAD 指
三、Git团队协作机制
团队内协作
push代码的前提是需要将push代码的人员提前拉我们的团队里面。
跨团队协作
四、GitHub操作
创建远程库&创建别名
创建远程库的时候最好名字和本地库名字相同
https://github.com/forgetc77/git-demo.git
远程仓库操作
命令名称 | 作用 |
---|---|
git remote -v | 查看当前所有远程地址别名 |
git remote add 别名 远程地址 | 起别名 |
git push 别名(或者链接)分支 | 推送本地分支上的内容到远程仓库 |
git clone 远程地址 | 将远程仓库的内容克隆到本地 |
git pull 远程库地址别名 远程分支名 | 将远程仓库对于分支最新内容拉下来后与 当前本地分支直接合并 |
创建别名
基本语法:
git remote -v
查看当前所有远程地址别名git remote add 别名 远程地址
案例:
#查看远程库名字
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (hot-fix)
$ git remote -v
#给远程库起别名
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (hot-fix)
$ git remote add git-demo https://github.com/forgetc77/git-demo.git
#再次查看别名
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (hot-fix)
$ git remote -v
git-demo https://github.com/forgetc77/git-demo.git (fetch)
git-demo https://github.com/forgetc77/git-demo.git (push)
#创建远程库
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git remote add gitTest https://github.com/rememberc77/gitTest.git
推送本地库到远程库
基本语法:git push 别名 分支
#推送master分支到本地库
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git push gitTest master
fatal: unable to access 'https://github.com/rememberc77/gitTest.git/': Failed to connect to github.com port 443 after 21027 ms: Couldn't connect to server
#上传成功
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git push gitTest master
Enumerating objects: 18, done.
Counting objects: 100% (18/18), done.
Delta compression using up to 12 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (18/18), 1.32 KiB | 675.00 KiB/s, done.
Total 18 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/rememberc77/gitTest.git
* [new branch] master -> master
#再次查看本地库状态,这里发现status状态没变,因为拉起动作会自动提交
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git status
On branch master
nothing to commit, working tree clean
推送的时候最小单位是分支,要记得把分支名跟在最后
拉取远程库到本地库
基本语法:git pull 别名 分支
在Github上修改hello.txt文件,并提交。
#从gitTest拉取到master分支上
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git pull gitTest master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 662 bytes | 110.00 KiB/s, done.
From https://github.com/rememberc77/gitTest
* branch master -> FETCH_HEAD
067d86e..ab1ef8c master -> gitTest/master
Updating 067d86e..ab1ef8c
Fast-forward
hello.txt | 1 +
1 file changed, 1 insertion(+)
克隆远程库到本地
基本语法:git clone 远程地址
在远程库获取地址URL
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-lhc
$ git clone https://github.com/rememberc77/gitTest.git
Cloning into 'gitTest'...
remote: Enumerating objects: 21, done.
remote: Counting objects: 100% (21/21), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 21 (delta 2), reused 17 (delta 1), pack-reused 0
Receiving objects: 100% (21/21), done.
Resolving deltas: 100% (2/2), done.
公共库的克隆是不需要账号的
clone 会做如下操作:
-
拉取代码。
-
初始化本地仓库。
克隆后生成一个同样的文件:
-
创建别名。
#查看别名,默认clone时候自动生成别名
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-lhc/gitTest (master)
$ git remote -v
origin https://github.com/rememberc77/gitTest.git (fetch)
origin https://github.com/rememberc77/gitTest.git (push)
团队内协作
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-lhc/gitTest (master)
$ vim hello.txt
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-lhc/gitTest (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-lhc/gitTest (master)
$ git add hello.txt
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-lhc/gitTest (master)
$ git commit -m "lhc commit" hello.txt
[master 946e1cb] lhc commit
1 file changed, 2 insertions(+)
#再次提交但是登录了也不行
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-lhc/gitTest (master)
$ git push https://github.com/rememberc77/gitTest.git master
remote: Permission to rememberc77/gitTest.git denied to forgetc77.
fatal: unable to access 'https://github.com/rememberc77/gitTest.git/': The requested URL returned error: 403
这里登录之后再次提交还是不行,这里需要将其添加到团队里面
进入这个仓库的setting,设置collaboration
复制邀请函:
另一位用户登录到这个网站:复制邀请链接,同意
再次尝试推送:
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-lhc/gitTest (master)
$ git push https://github.com/rememberc77/gitTest.git master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 270 bytes | 270.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/rememberc77/gitTest.git
ab1ef8c..946e1cb master -> master
跨团队协作
一、将远程仓库的地址复制发给邀请跨团队协作的人,比如东方不败。
二、在东方不败的 GitHub 账号里的地址栏复制收到的链接,然后点击 网页右上方的Fork按钮,将项目叉到自己的本地仓库。
叉成功后可以看到当前仓库信息。
三、东方不败就可以在线编辑叉取过来的文件。
四、编辑完毕后,填写描述信息并点击左下角绿色按钮提交。
五、接下来点击上方的 Pull 请求,并创建一个新的请求。
六、回到岳岳 GitHub 账号可以看到有一个 Pull request 请求。
进入到聊天室,可以讨论代码相关内容。
七、如果代码没有问题,可以点击 Merge pull reque 合并代码。
免密SSH
我们可以看到远程仓库中还有一个 SSH 的地址,因此我们也可以使用 SSH 进行访问。
先到用户的主页目录,删除.ssh文件夹(如果没有.ssh文件夹,忽略此步):
19127@DESKTOP-IG7JKN3 MINGW64 ~
$ ssh-keygen -t rsa -C 邮箱
连敲三次回车
生成两个文件公钥和私钥
19127@DESKTOP-IG7JKN3 MINGW64 ~
$ cd .ssh/
19127@DESKTOP-IG7JKN3 MINGW64 ~/.ssh
$ ls
id_rsa id_rsa.pub
19127@DESKTOP-IG7JKN3 MINGW64 ~/.ssh
$ cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDdPsaNHbe6XdOk7BnfgoavDhux5wFgAKc8TA3u4VxMwv9rDErQetzaHfHAiAxMHMaG4NGmnRT7WiNPj6OIvXoJlLnC+FB8NM9VraKYyrECQq+rhUFdQ1XGBKrziaQ1a/8NiZzmvp2hB+KVA5YKC0vJqi4crJ/L8GrKKFJQQ4TzCu9ii3xEy3J/Hu8JyJUv4q0i+n+bNtGt7Z4gQxsqK6qaBwdjwvZsi3y3a714gYubDJx/M//9A4q8zB9kbSCvE/W2wFn1qqqO94DoImtWlSR211+4WyWXhba8KK3RpU34yfcF14YOS7FP8Cmq8NigatHahrIViNuVO7W2aKeKro42KjrmlxlidBFM2FTjR6rJc/7dAJhpi8RJ/s54DWAMBEamURXF5WfbnV2jnOGyZs11jiFTXds5pxVDIy/F4Jet5OhNqAzcd+toAMsKkf02SIMpTFLHmQ2sviAIrDbUAXeXzvZ+otSm9K17iXzRwIFTfILnqiyU05PJydOQ0Dg2yZ0= 1912733027@qq.com
然后,将生成的公钥添加至Github账号SSH设置
此时再次查看ssh链接,这里已经不再报警告了
#拉取代码
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git pull git@github.com:rememberc77/gitTest.git master
The authenticity of host 'github.com (140.82.113.4)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), 250 bytes | 20.00 KiB/s, done.
From github.com:rememberc77/gitTest
* branch master -> FETCH_HEAD
Updating ab1ef8c..946e1cb
Fast-forward
hello.txt | 2 ++
1 file changed, 2 insertions(+)
#再次编辑
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ vim hello.txt
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git add hello.txt
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: hello.txt
#再次提交
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git commit -m "ssh commit test" hello.txt
[master 549423a] ssh commit test
1 file changed, 2 insertions(+)
#再次提交
19127@DESKTOP-IG7JKN3 MINGW64 /d/softWorkSpace/Git-Space/git-demo (master)
$ git push git@github.com:rememberc77/gitTest.git master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 270 bytes | 270.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:rememberc77/gitTest.git
946e1cb..549423a master -> master
说明这个账号可以用ssh提交
五、IDEA集成
配置 Git 忽略文件
与项目的实际功能无关,不参与服务器上部署运行。把它们忽略掉能够屏蔽 IDE 工具之
间的差异。例如,Maven工程根据src生成的target。
创建忽略规则文件 xxxx.ignore(前缀名随便起,建议是 git.ignore),这个文件的存放位置原则上在哪里都可以,为了便于让~/.gitconfig 文件引用,建议也放在用户家目录下。
git.ignore 文件模版内容如下:
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
.classpath
.project
.settings
target
.idea
*.iml
在.gitconfig 文件中引用忽略配置文件(此文件在 Windows 的家目录中)
[user]
name = sgc2001
email = 1912733027@qq.com
[credential "https://gitee.com"]
provider = generic
[core]
excludesfile = C:/Users/19127/git.ignore
注意:这里要使用“正斜线(/)”,不要使用“反斜线(\)”
在IDEA配置Git程序
在菜单栏File->Setting->搜索栏搜Git,配置Git的安装路径。
Git_初始化&添加&提交
先创建一个名叫git-Test的Maven工程。
初始化Git
在菜单栏VCS -> Create Git Repository
选择要创建 Git 本地仓库的工程,也就是git-Test工程,然后添加OK。