Git常用命令用法

参考视频:真的是全能保姆

git、github 保姆级教程入门,工作和协作必备技术,github提交pr - pull request_哔哩哔哩_bilibili

1.Git初始化

首先设置名称和邮箱。然后初始化一下,然后就创建了一个空的Git仓库。 

PS D:\golang\otherProjects\ginchat> git version
git version 2.42.0.windows.1
PS D:\golang\otherProjects\ginchat> git config --global user.name "Orange"
PS D:\golang\otherProjects\ginchat> git config --global user.email "[email protected]"
PS D:\golang\otherProjects\ginchat> git config
PS D:\golang\otherProjects\ginchat> git init
Initialized empty Git repository in D:/golang/otherProjects/ginchat/.git/

可以在当前项目目录里发现一个隐藏文件夹.git,这个文件里保存项目的每个版本和变化

2.Git暂存、提交、日志

1)第一版本

git提交到暂存区

git add + 想存储的文件
git add . //存储当前目录所有文件

2) 

 git提交,之后会出现vim编辑器编写文件说明,此处是linux命令常规操作

git commit

如果在命令行编辑,遵守vim规范。如果在vscode编辑,编辑完关闭即可。随后就自动开始提交过程。

3) 

 git log查看提交日志信息,此时完成了一个版本的提交

PS D:\golang\otherProjects\ginchat> git log
commit 078d952815dcec6a0c25e668ad1f5ad72ddb6874 (HEAD -> master)
Author: Orange <[email protected]>
Date:   Tue Sep 5 03:03:19 2023 +0800

    版本v0.1,第一次提交
PS D:\golang\otherProjects\ginchat> 

4)第二版本

vscode版本新增文件显示绿色

修改一个文件会显示橙色M

5)第二次提交

第二次暂存和提交。用-m + 说明的形式可以直接代替上面的vim文档说明编写。

PS D:\golang\otherProjects\ginchat> git add .
PS D:\golang\otherProjects\ginchat> git commit -m "v0.2第二次提交"
[master a1a2abf] v0.2第二次提交
 1 file changed, 1 insertion(+), 1 deletion(-)

git log查看 

PS D:\golang\otherProjects\ginchat> git log
commit a1a2abf9894af11d40fcc5dc76775e0c793c8f30 (HEAD -> master)
Author: Orange <[email protected]>
Date:   Tue Sep 5 03:13:00 2023 +0800

    v0.2第二次提交

commit 078d952815dcec6a0c25e668ad1f5ad72ddb6874
Author: Orange <[email protected]>
Date:   Tue Sep 5 03:03:19 2023 +0800
:

6)第三次提交

一般编写说明按照如下的规范来写

写明修正了什么文件,修改了什么内容

 git commit -m "fix(version):change content"

PS D:\golang\otherProjects\ginchat> git add .
warning: in the working copy of 'gitLog', LF will be replaced by CRLF the next time Git touches it
PS D:\golang\otherProjects\ginchat> git commit -m "fix(version):change content"
[master 305c688] fix(version):change content
 1 file changed, 11 insertions(+)
 create mode 100644 gitLog

7)

vscode自带版本控制,左侧工具栏可以看到当前版本和上一版本对比。

点击勾号可以直接暂存和提交

3.回退版本并清空之后版本

回退版本,版本id可以在日志里看

git reset --hard + 版本id

回退之后项目变成当时版本,并且之后的日志都清空了,相当于回到那个时间点。

PS D:\golang\otherProjects\ginchat> git reset --hard 305c688213cd0321c23112e48fba0ffb8da566c3
HEAD is now at 305c688 fix(version):change content

4.切换版本和分支,不会清除其他版本

创建分支,有点像数据结构的树,加一个分支

git branch + 版本号

PS D:\golang\otherProjects\ginchat> git branch 0.3
PS D:\golang\otherProjects\ginchat> git branch 0.4
查看分支和切换分支,master是主支,其他是分支
PS D:\golang\otherProjects\ginchat> git branch -a 
  0.3
  0.4
* master
PS D:\golang\otherProjects\ginchat> git checkout 0.3
Switched to branch '0.3'
M       version.txt
PS D:\golang\otherProjects\ginchat> git checkout master
Switched to branch 'master'
M       version.txt

5.合并分支

把分支版本合并到主版本,如图,每个人用一个分支版本开发自己的功能,最后老板把五个版本一合并就获得所有功能。

 git merge +版本号

PS D:\golang\otherProjects\ginchat> git merge 0.3
Already up to date.

所以为了方便团队开发和版本合并,就会建立服务器作为git仓库

6.上传到Github

git push 命令 | 菜鸟教程 (runoob.com)

echo "# Ginchat" >> README.md
git init
git add README.md
git commit -m "first commit" 
git branch -M main   //创建main分支,并设为主枝
git remote add origin https://github.com/BigBigOrangeSama/Ginchat.git  //添加远程仓库地址设代号为origin
git push -u origin main  //把项目上传到github的仓库里
//然后会让你输入账号密码

上传结果:上传成功,到github上查看

PS D:\golang\otherProjects\ginchat> git push -u origin master
info: please complete authentication in your browser...
Enumerating objects: 1260, done.
Counting objects: 100% (1260/1260), done.
Delta compression using up to 16 threads
Compressing objects: 100% (1228/1228), done.
Writing objects: 100% (1260/1260), 79.44 MiB | 2.88 MiB/s, done.
Total 1260 (delta 153), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (153/153), done.
To https://github.com/BigBigOrangeSama/Ginchat.git
 * [new branch]      master -> master
branch 'master' set up to track 'origin/master'.

7.项目克隆到本地

1)自己仓库下载到本地

如果你要参与某人的开源项目,把他的项目fork到自己的仓库,然后复制项目http链接。

用vscode打开一个空文件夹,在这个文件夹下的终端使用git clone命令

git clone + 项目http链接 + .    点代表当前文件夹

PS D:\golang\otherProjects\Git-demo> git clone https://github.com/BigBigOrangeSama/Git-demo.git .
Cloning into '.'...
remote: Enumerating objects: 1632, done.
remote: Counting objects: 100% (466/466), done.
remote: Compressing objects: 100% (276/276), done.
remote: Total 1632 (delta 207), reused 288 (delta 175), pack-reused 1166
Receiving objects: 100% (1632/1632), 284.21 KiB | 973.00 KiB/s, done.
Resolving deltas: 100% (439/439), done.

2)加入别人的仓库链接

做别人的项目的时候加入上游代码库http链接

PS D:\golang\otherProjects\Git-demo> git remote add upstream https://github.com/midorg-com/re01.git
PS D:\golang\otherProjects\Git-demo> git remote -v                                                
origin  https://github.com/BigBigOrangeSama/Git-demo.git (fetch)
origin  https://github.com/BigBigOrangeSama/Git-demo.git (push)
upstream        https://github.com/midorg-com/re01.git (fetch)
upstream        https://github.com/midorg-com/re01.git (push)

创建一个分支,然后随便写一点东西,然后add和commit

然后提交到自己fork的那个仓库里去

PS D:\golang\otherProjects\Git-demo> git checkout -b tx
Switched to a new branch 'tx'
PS D:\golang\otherProjects\Git-demo> git add .
PS D:\golang\otherProjects\Git-demo> git commit -m "add(test)"
On branch tx
nothing to commit, working tree clean
PS D:\golang\otherProjects\Git-demo> git add .
PS D:\golang\otherProjects\Git-demo> git commit -m "add(1111):xxxx"
[tx 617a1e7] add(1111):xxxx
 1 file changed, 3 insertions(+)
 create mode 100644 members/1111.json

PS D:\golang\otherProjects\Git-demo> git remote -v
origin  https://github.com/BigBigOrangeSama/Git-demo.git (fetch)
origin  https://github.com/BigBigOrangeSama/Git-demo.git (push)
upstream        https://github.com/midorg-com/re01.git (fetch)
upstream        https://github.com/midorg-com/re01.git (push)

上传到仓库

PS D:\golang\otherProjects\Git-demo> git push origin tx            
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 16 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 365 bytes | 365.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote:
remote: Create a pull request for 'tx' on GitHub by visiting:
remote:      https://github.com/BigBigOrangeSama/Git-demo/pull/new/tx
remote:
To https://github.com/BigBigOrangeSama/Git-demo.git
 * [new branch]      tx -> tx

3)合并

点击PR拉取请求

 创建拉取请求,让项目主人把你修改的分支合并到主枝里去。显示able to merge就是可以提交

然后写上修改信息提交即可,等待项目主人合并

4)如果显示不能pr()

没有这个able to merge

可能是这段时间开源项目已经commit过了,导致版本不一致,需要你更新合并最新upstream链接,然后再提交。

 git fetch upstream

git merge 最新分支

git push

PS D:\golang\otherProjects\Git-demo> git fetch upstream
From https://github.com/midorg-com/re01
 * [new branch]      main       -> upstream/main
PS D:\golang\otherProjects\Git-demo> git merge upstream/main 
Already up to date.
PS D:\golang\otherProjects\Git-demo> git push xxxxxxx   xxxx

猜你喜欢

转载自blog.csdn.net/m0_50973548/article/details/132683109
今日推荐