Git & Github操作指南

网友投稿 691 2022-11-30

Git & Github操作指南

Git & Github操作指南

完成代码,不是一蹴而就的工作,需要你不断去调试,重构,迭代。有些时候还需要你回到更早的版本去再去试错和优化

在这个过程当中,git可以非常方便的完成代码版本管理的工作。

所以不光要把代码在电脑上面写好,还要同步到github和大家共享。这样可以互相交流和学习

什么是版本控制

git对各个代码文件进行版本控制,毕竟项目的代码总是在不断的迭代和优化,这中间其实会经历一个又一个的版本,这就需要一个高效的版本控制工具将其管理起来。

创建一个目录,并且将这个目录作为项目目录,和这个项目相关的所有文件都会放到这个目录里面。

lulei@luleideMacBook-Pro awesomeProject1 % lsgo.mod main.golulei@luleideMacBook-Pro awesomeProject1 % pwd/Users/lulei/go/src/awesomeProject1

接下来初始化这个仓库

lulei@luleideMacBook-Pro awesomeProject1 % ls -altotal 16drwxr-xr-x 5 lulei staff 160 5 24 11:11 .drwxr-xr-x 6 lulei staff 192 5 23 08:09 ..drwxr-xr-x 6 lulei staff 192 5 24 07:42 .idea-rw-r--r-- 1 lulei staff 32 5 18 09:41 go.mod-rw-r--r-- 1 lulei staff 373 5 24 11:11 main.golulei@luleideMacBook-Pro awesomeProject1 % git inithint: Using 'master' as the name for the initial branch. This default branch namehint: is subject to change. To configure the initial branch name to use in allhint: of your new repositories, which will suppress this warning, call:hint: hint: git config --global init.defaultBranch hint: hint: Names commonly chosen instead of 'master' are 'main', 'trunk' andhint: 'development'. The just-created branch can be renamed via this command:hint: hint: git branch -m Initialized empty Git repository in /Users/lulei/go/src/awesomeProject1/.git///可以看到多了.git这个目录lulei@luleideMacBook-Pro awesomeProject1 % ls -altotal 16drwxr-xr-x 6 lulei staff 192 5 24 15:22 .drwxr-xr-x 6 lulei staff 192 5 23 08:09 ..drwxr-xr-x 9 lulei staff 288 5 24 15:22 .gitdrwxr-xr-x 6 lulei staff 192 5 24 07:42 .idea-rw-r--r-- 1 lulei staff 32 5 18 09:41 go.mod-rw-r--r-- 1 lulei staff 373 5 24 11:11 main.go

上面初始化完成之后,还需要对仓库进行简单的配置。这样更好的能够让git记录到底谁在什么时候对相应的文件进行什么操作。

lulei@luleideMacBook-Pro awesomeProject1 % git config --global user.name "lulei"lulei@luleideMacBook-Pro awesomeProject1 % git config --global user.email "1239683670@qq.com"lulei@luleideMacBook-Pro awesomeProject1 % git config --global --listuser.name=luleiuser.email=1239683670@qq.com

当多人协作的时候,一起开发代码,那上面的配置是十分有意义的。

git简单操作

查看当前状态,可以看到没有东西需要提交

lulei@luleideMacBook-Pro awesomeProject1 % git statusOn branch masterNo commits yetUntracked files: (use "git add ..." to include in what will be committed) .idea/ go.mod main.gonothing added to commit but untracked files present (use "git add" to track)

可以看到git跟踪到变化啦,需要去提交

lulei@luleideMacBook-Pro awesomeProject1 % git add .lulei@luleideMacBook-Pro awesomeProject1 % git statusOn branch masterNo commits yetChanges to be committed: (use "git rm --cached ..." to unstage) new file: .idea/.gitignore new file: .idea/awesomeProject1.iml new file: .idea/modules.xml new file: go.mod new file: main.go

-m是做注释,每当发布新版本,做一个版本说明。提交新版本就使用这个命令。

lulei@luleideMacBook-Pro awesomeProject1 % git commit -m "go awesomeProject1 project"[master (root-commit) fd1c0b4] go awesomeProject1 project 5 files changed, 59 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/awesomeProject1.iml create mode 100644 .idea/modules.xml create mode 100644 go.mod create mode 100644 main.golulei@luleideMacBook-Pro awesomeProject1 % git status On branch masternothing to commit, working tree clean

从无到有新建文件,或者对原来的文件进行了修改。git add之后就从以修改变为以暂存的状态。最后一种状态就是以提交,运行commit命令之后。

这三种状态又对应三个区域。

新建文件和对文件的编辑操作都是在工作区域进行的, 当前这个文件是修改状态,并且处于工作区域。

git add 之后,文件状态就发生了变化,从工作区域移动到暂存区域,状态从以修改变为了以暂存。

git commit 之后就从暂存到提交状态了,从暂存区域移动到了仓库区域。

git log命令可以显示历史所有提交

lulei@luleideMacBook-Pro awesomeProject1 % git logcommit 18421665952d87455f170174c916c3095fced526 (HEAD -> master)Author: lulei <1239683670@qq.com>Date: Tue May 24 15:52:46 2022 +0800 go awesomeProject1 projectcommit fd1c0b41066849ba8bb00002b9adf9a78070ba78Author: lulei <1239683670@qq.com>Date: Tue May 24 15:45:45 2022 +0800 go awesomeProject1 project

将本地仓库同步到远程GitHub仓库

github就是开源项目的聚集平台,每个人都可以将自己代码发布到上面。

本地对代码做了修改和提交,怎么将其同步到GitHub远程仓库呢?

添加仓库有两种方式,一种是从头新建一个仓库,另外一种是fork别人的仓库。

仓库选择ssh协议, 因为使用awesomeProject1 % git remote add origin git@github.com:lovekeepcoding/test_git.gitlulei@luleideMacBook-Pro awesomeProject1 % git remoteorigin

配置公钥和私钥,私钥不要泄漏出去,公钥无所谓

lulei@luleideMacBook-Pro awesomeProject1 % ssh-keygen -t rsa -C "1239683670@qq.com"Generating public/private rsa key pair.Enter file in which to save the key (/Users/lulei/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /Users/lulei/.ssh/id_rsa.Your public key has been saved in /Users/lulei/.ssh/id_rsa.pub.The key fingerprint is:SHA256:WJSTIo3pVBP1z8EXZZhtV9Pq9cI1l+9xgW03EfEUTX0 1239683670@qq.comThe key's randomart image is:+---[RSA 3072]----+| =+ooo .@#|| = oo+. . o=E|| o . .... o +o*|| . o o +.B=|| . S oo.oO|| +.+|| oo|| .|| |+----[SHA256]-----+lulei@luleideMacBook-Pro awesomeProject1 % cd /Users/lulei/.sshlulei@luleideMacBook-Pro .ssh % lsid_rsa 私钥 id_rsa.pub 公钥 known_hosts

将公钥里面的内容复制

lulei@luleideMacBook-Pro .ssh % cat id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDh/yd+c8pi10Sk83D3OqMwKN8WkxTw+TB6nAOhk4Eqqs1pTTq9dqT9H+cUL1oXVizHWeM6++AGxUWECDiF9Xswd9BSM665bgeDFWPsuDp8IfFhZVSzUP0WUXMyC5l9Iy5uuwKuZ3BeEzIyDBZX52/PN4PVCZHsGrfKWdZDDxS273SJtEM0qpSEKy0WMptyy6NWrM04B++CUmH7FVwWYydWs40euhzjs+CT2l2jboxbJ2eyHfM4Px6QyA10vfwhVN6hnTdRvTI7DFN2l7ZRWuWilDx3c7LWVoLngnU5BnwLlzM54u2ZnIpdHt9V0EjQtCZZqhgtpn4sfBK5shOzSsGTIs4KIDxQMbdIiJNOtxGksb9vogHceg2ZlsXOv5ItGiwOqH2Xzn6Yqm+jkZS+o/x/igHJ62bvYf+rpD+/tCjsRJWLmP1OtTkQptBGPB1bGG60CgBLz/EZtHunzObc9buy5eR4g5dE6tq4q69h8L3zIiYrG8fIqZuZZ/oi0= 1239683670@qq.com

成功通过认证,认证成功之后切换到git仓库

lulei@luleideMacBook-Pro awesomeProject1 % ssh -T git@github.com The authenticity of host 'github.com (20.205.243.166)' can't be established.ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.Are you sure you want to continue connecting (yes/no/[fingerprint])? yesWarning: Permanently added 'github.com,20.205.243.166' (ECDSA) to the list of known hosts.Hi lovekeepcoding! You've successfully authenticated, but GitHub does not provide shell access.

lulei@luleideMacBook-Pro awesomeProject1 % git remote -vorigin (fetch)origin (push)lulei@luleideMacBook-Pro awesomeProject1 % git remote remove originlulei@luleideMacBook-Pro awesomeProject1 % git remote add origin git@github.com:lovekeepcoding/test_git.gitlulei@luleideMacBook-Pro awesomeProject1 % git remote -v origin git@github.com:lovekeepcoding/test_git.git (fetch)origin git@github.com:lovekeepcoding/test_git.git (push)lulei@luleideMacBook-Pro awesomeProject1 % git push -u origin main Enumerating objects: 12, done.Counting objects: 100% (12/12), done.Delta compression using up to 8 threadsCompressing objects: 100% (11/11), done.Writing objects: 100% (12/12), 1.62 KiB | 1.62 MiB/s, done.Total 12 (delta 1), reused 0 (delta 0), pack-reused 0remote: Resolving deltas: 100% (1/1), done.To github.com:lovekeepcoding/test_git.git * [new branch] main -> mainBranch 'main' set up to track remote branch 'main' from 'origin'.

如果再修改了内容,add commit push这几下就行了

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:小程序开发陕西(陕西小程序官网)
下一篇:传输层 可靠传输 重传与确认 停止等待协议工作原理
相关文章

 发表评论

暂时没有评论,来抢沙发吧~