Git & Github操作指南

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

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

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

什么是版本控制


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

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

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

接下来初始化这个仓库

lulei@luleideMacBook-Pro awesomeProject1 % ls -al
total 16
drwxr-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.go


lulei@luleideMacBook-Pro awesomeProject1 % git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint: 	git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint: 	git branch -m <name>
Initialized empty Git repository in /Users/lulei/go/src/awesomeProject1/.git/


//可以看到多了.git这个目录
lulei@luleideMacBook-Pro awesomeProject1 % ls -al
total 16
drwxr-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 .git
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.go

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

lulei@luleideMacBook-Pro awesomeProject1 % git config --global user.name "lulei"

lulei@luleideMacBook-Pro awesomeProject1 % git config --global user.email "[email protected]"

lulei@luleideMacBook-Pro awesomeProject1 % git config --global --list
user.name=lulei
[email protected]

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

扫描二维码关注公众号,回复: 14220466 查看本文章

git简单操作


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

lulei@luleideMacBook-Pro awesomeProject1 % git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	.idea/
	go.mod
	main.go

nothing added to commit but untracked files present (use "git add" to track)

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

lulei@luleideMacBook-Pro awesomeProject1 % git add .
lulei@luleideMacBook-Pro awesomeProject1 % git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." 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.go


lulei@luleideMacBook-Pro awesomeProject1 % git status                                
On branch master
nothing to commit, working tree clean

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

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

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

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

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

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

lulei@luleideMacBook-Pro awesomeProject1 % git log
commit 18421665952d87455f170174c916c3095fced526 (HEAD -> master)
Author: lulei <[email protected]>
Date:   Tue May 24 15:52:46 2022 +0800

    go awesomeProject1 project

commit fd1c0b41066849ba8bb00002b9adf9a78070ba78
Author: lulei <[email protected]>
Date:   Tue May 24 15:45:45 2022 +0800

    go awesomeProject1 project

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


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

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

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

仓库选择ssh协议, 因为使用https协议的话,在同步本地仓库和远程仓库的时候,都需要输入用户名和密码,使用ssh协议只需要一开始配置好公钥和私钥就行了。

现在是需要将本地仓库同步到远程仓库

给我们的本地仓库添加远程仓库

lulei@luleideMacBook-Pro awesomeProject1 % git remote add origin [email protected]:lovekeepcoding/test_git.git

lulei@luleideMacBook-Pro awesomeProject1 % git remote
origin

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

lulei@luleideMacBook-Pro awesomeProject1 % ssh-keygen -t rsa -C "[email protected]"
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 [email protected]
The 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/.ssh
lulei@luleideMacBook-Pro .ssh % ls
id_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= [email protected]

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

lulei@luleideMacBook-Pro awesomeProject1 % ssh -T [email protected]                            
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])? yes
Warning: 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 -v
origin	https://github.com/lovekeepcoding/test_git.git (fetch)
origin	https://github.com/lovekeepcoding/test_git.git (push)


lulei@luleideMacBook-Pro awesomeProject1 % git remote remove origin


lulei@luleideMacBook-Pro awesomeProject1 % git remote add origin [email protected]:lovekeepcoding/test_git.git

lulei@luleideMacBook-Pro awesomeProject1 % git remote -v           
origin	[email protected]:lovekeepcoding/test_git.git (fetch)
origin	[email protected]: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 threads
Compressing 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 0
remote: Resolving deltas: 100% (1/1), done.
To github.com:lovekeepcoding/test_git.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

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

猜你喜欢

转载自blog.csdn.net/qq_34556414/article/details/124947210