Git学习

56 阅读4分钟

git是一种源码管理系统(source code management,缩写为SCM)。它对当前文件提供版本管理功能,核心思想是对当前文件建立一个对象数据库(object database),将历史版本信息存放在这个数据库中。

示例命令

git init:新建一个git库
git status:查看目前状态
git add <文件名>:添加文件从工作区到暂存区
git commit -m “提示信息”:从暂存区提交到代码仓库
git log:查看提交commit的信息
git remote add origin https://github.com/Bubblecqq/test.git : 
git push -u origin master:将本地的master分支推送到远程origin主机,-u参数表示记住对应关系,下次可以直接git push推送。
git pull origin master:将远程主机origin的代码取回本地,与本地的master分支合并
git diff HEAD:查看与上一次commit的区别

发布一个版本

为当前分支打上版本号。

$ git tag -a [VERSION] -m "released [VERSION]"
$ git push origin [VERSION]
PS D:\development\Go Project\learningGit> git tag -a v1 -m "released v1" 
PS D:\development\Go Project\learningGit> git push origin master v1
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 152 bytes | 152.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To https://github.com/Bubblecqq/test.git
 * [new tag]         v1 -> v1

分支

分支是 Git 最重要的概念之一,也是最常用的操作之一。几乎所有 Git 操作流程都离不开分支。

git branch命令可以列出本地的所有分支。

$ git branch
PS D:\development\Go Project\learningGit> git branch
* master

创建分支

使用git branch [分支名]来创建一个分支,并保持分支为原来的分支

$ git branch test1
PS D:\development\Go Project\learningGit> git branch test1
PS D:\development\Go Project\learningGit> git branch
* master
  test1

在远程主机origin上创建一个MyBranch的分支,并与本地的同名分支建立追踪关系。

$ git push -u origin test1
PS D:\development\Go Project\learningGit> git push -u origin test1
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
remote: 
remote: Create a pull request for 'test1' on GitHub by visiting:
remote:      https://github.com/Bubblecqq/test/pull/new/test1
remote:
To https://github.com/Bubblecqq/test.git
 * [new branch]      test1 -> test1
branch 'test1' set up to track 'origin/test1'.

切换分支

切换到test1分支,当前的工作区会变为test1分支的内容。

PS D:\development\Go Project\learningGit> git checkout test1
A       pkg/test2.go
Switched to branch 'test1'
Your branch is up to date with 'origin/test1'.

分支改名

$ git branch -m test2

这里我们将test1的分支改名为test2,我们先使用checkout切换分支,在执行git branch -m [新分支名],会将当前处在的分支进行修改

PS D:\development\Go Project\learningGit> git checkout test1
A       pkg/test2.go
Switched to branch 'test1'
Your branch is up to date with 'origin/test1'.
PS D:\development\Go Project\learningGit> git branch
  master
* test1
PS D:\development\Go Project\learningGit> git branch -m test2
PS D:\development\Go Project\learningGit> git branch
  master
* test2

删除分支

$ git branch -d test2

我们尝试直接使用git branch -d test2删除当前处在的分支

PS D:\development\Go Project\learningGit> git branch -d test2
error: cannot delete branch 'test2' used by worktree at 'D:/development/Go Project/learningGit'
PS D:\development\Go Project\learningGit> git checkout master
A       pkg/test2.go
Switched to branch 'master'
PS D:\development\Go Project\learningGit> git branch -d test2
Deleted branch test2 (was ff8e6e9).
PS D:\development\Go Project\learningGit> git branch
* master

直接删除当前处在的分支会直接报错,需要先切换到其他分支在进行删除。

基于master分支创建一个新的test1分支,新的test1分支将成为当前的工作区。

git checkout -b test1 master

可以看到,同时会切换到新的分支作为工作区。

PS D:\development\Go Project\learningGit> git checkout -b test1 master
A       pkg/test2.go
Switched to a new branch 'test1'
PS D:\development\Go Project\learningGit> git branch
  master
* test1

git branch

git branch是分支操作命令。

# 列出所有本地分支
$ git branch

# 列出所有本地分支和远程分支
$ git branch -a

1)新建一个分支

直接在git branch后面跟上分支名,就表示新建该分支。

$ git branch test2

2)删除分支

-d参数用来删除一个分支,前提是该分支没有未合并的变动。

$ git branch -d <分支名>

强制删除一个分支,不管有没有未合并变化。

$ git branch -D <分支名>

git checkout

git checkout命令有多种用途。

(1)用来切换分支。

$ git checkout

上面命令表示回到先前所在的分支。

$ git checkout develop

上面命令表示切换到develop分支。

(2)切换到指定快照(commit)

$ git checkout <commitID>

git init

git init命令将当前目录转为git仓库。

它会在当前目录下生成一个.git子目录,在其中写入git的配置和项目的快照。

git pull

# 合并指定分支到当前分支
$ git pull . topic/branch

即使当前分支有没有 commit 的变动,也可以使用git pull从远程拉取分支。

git remote

为远程仓库添加别名。

$ git remote add origin https://github.com/Bubblecqq/test.git

# 显示所有的远程主机
$ git remote -v

# 列出某个主机的详细信息
$ git remote show name

git clone

git clone命令用于克隆远程分支。

$ git clone https://github.com/Bubblecqq/test.git

git commit

git commit命令用于将暂存区中的变化提交到仓库区。

-m参数用于指定 commit 信息,是必需的。如果省略-m参数,git commit会自动打开文本编辑器,要求输入。

$ git commit -m "message"

git commit命令可以跳过暂存区,直接将文件从工作区提交到仓库区。

$ git commit <filename>  -m "message"

上面命令会将工作区中指定文件的变化,先添加到暂存区,然后再将暂存区提交到仓库区。

git push

# 上传本地指定分支到远程仓库
$ git push [remote] [branch]

git stash

git stash命令用于暂时保存没有提交的工作。运行该命令后,所有没有commit的代码,都会暂时从工作区移除,回到上次commit时的状态。

PS D:\development\Go Project\learningGit> ls


    目录: D:\development\Go Project\learningGit


Mode                 LastWriteTime         Length Name                                                                                                          
----                 -------------         ------ ----                                                                                                          
d-----        2024/11/28     20:16                .idea                                                                                                         
d-----        2024/11/28     21:47                pkg                                                                                                           
d-----        2024/11/28     19:56                test                                                                                                          
-a----        2024/11/28     19:58            392 .gitignore                                                                                                    
-a----        2024/11/28     20:16             19 go.mod                                                                                                        
-a----        2024/11/28     19:58          11558 LICENSE                                                                                                       
-a----        2024/11/28     19:58             22 README.md                                                                                                     


PS D:\development\Go Project\learningGit> git status
On branch test1
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   pkg/test2.go

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:   pkg/test2.go

PS D:\development\Go Project\learningGit> git stash
Saved working directory and index state WIP on test1: ff8e6e9 first
PS D:\development\Go Project\learningGit> ls


    目录: D:\development\Go Project\learningGit


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        2024/11/28     20:16                .idea
d-----        2024/11/28     22:21                pkg
d-----        2024/11/28     19:56                test
-a----        2024/11/28     19:58            392 .gitignore
-a----        2024/11/28     20:16             19 go.mod
-a----        2024/11/28     19:58          11558 LICENSE
-a----        2024/11/28     19:58             22 README.md


PS D:\development\Go Project\learningGit> ls pkg


    目录: D:\development\Go Project\learningGit\pkg


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        2024/11/28     20:16             30 test1.go

列出所有暂时保存的工作

# 列出所有暂时保存的工作
$ git stash list
PS D:\development\Go Project\learningGit> git stash list
stash@{0}: WIP on test1: ff8e6e9 first

恢复最近一次stash的文件

# 恢复最近一次stash的文件
git stash pop
PS D:\development\Go Project\learningGit> git stash pop
On branch test1
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   pkg/test2.go

Dropped refs/stash@{0} (fabf9bb166c5aa94e584a4abe6bc21a5e41f54b7)
PS D:\development\Go Project\learningGit> ls pkg


    目录: D:\development\Go Project\learningGit\pkg


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        2024/11/28     20:16             30 test1.go
-a----        2024/11/28     22:24             31 test2.go

丢弃最近一次stash的文件

PS D:\development\Go Project\learningGit> git stash
Saved working directory and index state WIP on test1: ff8e6e9 first
PS D:\development\Go Project\learningGit> git stash list
stash@{0}: WIP on test1: ff8e6e9 first
PS D:\development\Go Project\learningGit> git stash drop
Dropped refs/stash@{0} (11378ebb9cd8d2a5d640edd4297a749704da4532)
PS D:\development\Go Project\learningGit> git stash list

git reset

git reset命令用于将当前分支指向另一个位置。

# 将当期分支的指针倒退三个 commit,
# 并且会改变暂存区
$ git reset HEAD~3

# 倒退指针的同时,不改变暂存区
$ git reset --soft HEAD~3

# 倒退指针的同时,改变工作区
$ git reset --hard HEAD~3

参考地址

[1] www.bookstack.cn/read/git-tu…