概述
安装Git
下载官方网站,下载后安装包样式:双击安装,安装成功后右键文件会有如下选项
证明安装成功。
基本配置
- 设置用户信息,桌面右键,选择Git bash here
hecheng@LAPTOP-OJQFEH00 MINGW64 ~/Desktop
$ git config --global user.name "hecheng"
hecheng@LAPTOP-OJQFEH00 MINGW64 ~/Desktop
$ git config --global user.email "[email protected]"
自己编写自己的用户名和邮箱,之后可以查看,效果如下
基本操作
- 创建文件——>提交到缓存区——>提交到仓库
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest
$ git init
Initialized empty Git repository in E:/GitTest/.git/
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ ll
total 8
drwxr-xr-x 1 hecheng 197121 0 7月 17 16:18 ./
drwxr-xr-x 1 hecheng 197121 0 7月 17 16:17 ../
drwxr-xr-x 1 hecheng 197121 0 7月 17 16:18 .git/
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ touch file01.txt
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
file01.txt
nothing added to commit but untracked files present (use "git add" to track)
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git add .
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: file01.txt
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git commit -m "add file01"
[master (root-commit) e205281] add file01
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file01.txt
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git status
On branch master
nothing to commit, working tree clean
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git log
commit e2052811960420feb66b8b7bef48980537ea859b (HEAD -> master)
Author: hecheng <[email protected]>
Date: Mon Jul 17 16:30:35 2023 +0800
add file01
- 修改文件后,文件又进入工作区
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git status
On branch 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: file01.txt
no changes added to commit (use "git add" and/or "git commit -a")
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git add .
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: file01.txt
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git commit -m "update file01"
[master 033fcd2] update file01
1 file changed, 1 insertion(+)
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git log
commit 033fcd2c7386e611bff68c325ab2c770e075f014 (HEAD -> master)
Author: hecheng <[email protected]>
Date: Mon Jul 17 16:36:21 2023 +0800
update file01
commit e2052811960420feb66b8b7bef48980537ea859b
Author: hecheng <[email protected]>
Date: Mon Jul 17 16:30:35 2023 +0800
add file01
- 版本回退
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git-log
* 033fcd2 (HEAD -> master) update file01
* e205281 add file01
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git reset --hard e205281
HEAD is now at e205281 add file01
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git-log
* e205281 (HEAD -> master) add file01
- 查看已删除的记录
$ git reflog
033fcd2 (HEAD -> master) HEAD@{0}: reset: moving to 033fcd2
e205281 HEAD@{1}: reset: moving to e205281
033fcd2 (HEAD -> master) HEAD@{2}: commit: update file01
e205281 HEAD@{3}: commit (initial): add file01
Git分支
- 查看,创建,切换分支
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git branch
* master
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git branch dev01
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git-log
* 033fcd2 (HEAD -> master, dev01) update file01
* e205281 add file01
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ touch .gitignore
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git add .
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: .gitignore
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git commit -m "add ignore file"
[master 4008424] add ignore file
1 file changed, 1 insertion(+)
create mode 100644 .gitignore
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git status
On branch master
nothing to commit, working tree clean
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git-log
* 4008424 (HEAD -> master) add ignore file
* 033fcd2 (dev01) update file01
* e205281 add file01
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git checkout dev01
Switched to branch 'dev01'
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev01)
$ git checkout master
Switched to branch 'master'
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git checkout -b dev02
Switched to a new branch 'dev02'
- 合并、删除分支
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev02)
$ git checkout master
Switched to branch 'master'
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git-log
* 4008424 (HEAD -> master, dev02) add ignore file
* 033fcd2 (dev01) update file01
* e205281 add file01
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git checkout dev01
Switched to branch 'dev01'
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev01)
$ touch file02.txt
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev01)
$ git add .
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev01)
$ git commit -m 'add file02'
[dev01 f2a85f1] add file02
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file02.txt
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev01)
$ git-log
* f2a85f1 (HEAD -> dev01) add file02
| * 4008424 (master, dev02) add ignore file
|/
* 033fcd2 update file01
* e205281 add file01
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev01)
$ git checkout master
Switched to branch 'master'
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git merge dev01
Merge made by the 'ort' strategy.
file02.txt | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file02.txt
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git-log
* c3ebaa2 (HEAD -> master) Merge branch 'dev01'
|\
| * f2a85f1 (dev01) add file02
* | 4008424 (dev02) add ignore file
|/
* 033fcd2 update file01
* e205281 add file01
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git branch -d dev02
Deleted branch dev02 (was 4008424).
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git-log
* c3ebaa2 (HEAD -> master) Merge branch 'dev01'
|\
| * f2a85f1 (dev01) add file02
* | 4008424 add ignore file
|/
* 033fcd2 update file01
* e205281 add file01
强制删除
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git-log
* 88d63ff (dev02) abcde
* b357906 (HEAD -> master) Merge branch 'dev'
|\
| * b6c3192 update file01 count-2
* | d9ed7d0 update file01 count-3
|/
* c3ebaa2 Merge branch 'dev01'
|\
| * f2a85f1 add file02
* | 4008424 add ignore file
|/
* 033fcd2 update file01
* e205281 add file01
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git branch -d dev02
error: The branch 'dev02' is not fully merged.
If you are sure you want to delete it, run 'git branch -D dev02'.
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git branch -D dev02
Deleted branch dev02 (was 88d63ff).
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git-log
* b357906 (HEAD -> master) Merge branch 'dev'
|\
| * b6c3192 update file01 count-2
* | d9ed7d0 update file01 count-3
|/
* c3ebaa2 Merge branch 'dev01'
|\
| * f2a85f1 add file02
* | 4008424 add ignore file
|/
* 033fcd2 update file01
* e205281 add file01
- 解决冲突
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git checkout -b dev
Switched to a new branch 'dev'
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev)
$ git status
On branch dev
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: file01.txt
no changes added to commit (use "git add" and/or "git commit -a")
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev)
$ git add .
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev)
$ git commit -m "update file01 count-2"
[dev b6c3192] update file01 count-2
1 file changed, 1 insertion(+), 1 deletion(-)
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev)
$ git-log
* b6c3192 (HEAD -> dev) update file01 count-2
* c3ebaa2 (master) Merge branch 'dev01'
|\
| * f2a85f1 add file02
* | 4008424 add ignore file
|/
* 033fcd2 update file01
* e205281 add file01
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (dev)
$ git checkout master
Switched to branch 'master'
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git add .
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git commit -m "update file01 count-3"
[master d9ed7d0] update file01 count-3
1 file changed, 1 insertion(+), 1 deletion(-)
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
Merge branch 'dev'
[master b357906] Merge branch 'dev'
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git-log
* b357906 (HEAD -> master) Merge branch 'dev'
|\
| * b6c3192 (dev) update file01 count-2
* | d9ed7d0 update file01 count-3
|/
* c3ebaa2 Merge branch 'dev01'
|\
| * f2a85f1 add file02
* | 4008424 add ignore file
|/
* 033fcd2 update file01
* e205281 add file01
- 分支创建原则
master,主分支,用于发行的版本,develop,开发的分支,feature(可以删除),功能的分支,hotfix,修复bug的分支,feature合并到develop,develop合并到master,用户反馈到bug,创建hotfix分支,写好修复代码后,分别合并到master分支和develop分支。
四、远程仓库
1.
创建码云账户,创建仓库,获取公匙。
再连接本地仓库与远程仓库(添加远程仓库:git remote add 远端名称 仓库路径),查看远程仓库,将代码推送到远程仓库(git push 远端名称 本地分支名:远端分支名),查看关联关系:git branch -vv
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git remote add origin [email protected]:hecheng11223/git_-test.git
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git remote
origin
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git push origin master
Enumerating objects: 22, done.
Counting objects: 100% (22/22), done.
Delta compression using up to 16 threads
Compressing objects: 100% (14/14), done.
Writing objects: 100% (22/22), 1.82 KiB | 621.00 KiB/s, done.
Total 22 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:hecheng11223/git_-test.git
* [new branch] master -> master
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git push origin master:master
Everything up-to-date
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git push --set-upstream origin master
Everything up-to-date
branch 'master' set up to track 'origin/master'.
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git push
Everything up-to-date
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/GitTest (master)
$ git branch -vv
* master b357906 [origin/master] Merge branch 'dev'
2.
从远程仓库克隆
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/Git_Study
$ git clone [email protected]:hecheng11223/git_-test.git hello-git
Cloning into 'hello-git'...
remote: Enumerating objects: 22, done.
remote: Counting objects: 100% (22/22), done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 22 (delta 3), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (22/22), done.
Resolving deltas: 100% (3/3), done.
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/Git_Study/hello-git (master)
$ git-log
* b357906 (HEAD -> master, origin/master, origin/HEAD) Merge branch 'dev'
|\
| * b6c3192 update file01 count-2
* | d9ed7d0 update file01 count-3
|/
* c3ebaa2 Merge branch 'dev01'
|\
| * f2a85f1 add file02
* | 4008424 add ignore file
|/
* 033fcd2 update file01
* e205281 add file01
从远程仓库抓取和拉取
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/Git_Study/hello-git (master)
$ git fetch
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), 229 bytes | 76.00 KiB/s, done.
From gitee.com:hecheng11223/git_-test
b357906..c9b33a5 master -> origin/master
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/Git_Study/hello-git (master)
$ git-log
* c9b33a5 (origin/master, origin/HEAD) add file03
* b357906 (HEAD -> master) Merge branch 'dev'
|\
| * b6c3192 update file01 count-2
* | d9ed7d0 update file01 count-3
|/
* c3ebaa2 Merge branch 'dev01'
|\
| * f2a85f1 add file02
* | 4008424 add ignore file
|/
* 033fcd2 update file01
* e205281 add file01
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/Git_Study/hello-git (master)
$ git merge origin/master
Updating b357906..c9b33a5
Fast-forward
file04.txt | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file04.txt
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/Git_Study/hello-git (master)
$ git pull
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), 208 bytes | 69.00 KiB/s, done.
From gitee.com:hecheng11223/git_-test
c9b33a5..418f89f master -> origin/master
Updating c9b33a5..418f89f
Fast-forward
file05.txt | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file05.txt
hecheng@LAPTOP-OJQFEH00 MINGW64 /e/Git_Study/hello-git (master)
$ git-log
* 418f89f (HEAD -> master, origin/master, origin/HEAD) add file05
* c9b33a5 add file03
* b357906 Merge branch 'dev'
|\
| * b6c3192 update file01 count-2
* | d9ed7d0 update file01 count-3
|/
* c3ebaa2 Merge branch 'dev01'
|\
| * f2a85f1 add file02
* | 4008424 add ignore file
|/
* 033fcd2 update file01
* e205281 add file01