Git 尝试
阅读原文时间:2023年07月11日阅读:2

1,下载Git

2,安装GIt

3,config :

git config --global user.name "mxb"
git config --global user.email "357670442@qq.com"

4,创建目录并且进行初始化

cd c:
mkdir GitRep
cd c:\GitRep
git init

5,创建readme.txt 并提交

Administrator@Mxb20200114 MINGW64 /c/GitRep (master)
$ git add readme.txt

Administrator@Mxb20200114 MINGW64 /c/GitRep (master)
$ git status
On branch master

No commits yet

Changes to be committed:
(use "git rm --cached …" to unstage)
new file: readme.txt

Administrator@Mxb20200114 MINGW64 /c/GitRep (master)
$ git commit -m "wrote a readme.txt"
[master (root-commit) 654c826] wrote a readme.txt
1 file changed, 2 insertions(+)
create mode 100644 readme.txt

Administrator@Mxb20200114 MINGW64 /c/GitRep (master)

6,常用操作:

$ git status
On branch master
Changes not staged for commit:
(use "git add …" to update what will be committed)
(use "git restore …" to discard changes in working directory)
modified: readme.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git diff readme.txt
diff --git a/readme.txt b/readme.txt
index 3a94755..3c2ffbf 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,3 @@
Git is a version control system.
-Git is free software.
\ No newline at end of file
+Git is free software.
+stupid boss.
\ No newline at end of file

7,查看提交log 和 版本回退

分别是,提交ID,提交的分支名称master,并且当前指针 HEAD 指向 master

提交作者,提交日期.

8,版本回退  git reset –hard  HEAD HEAD^ HEAD~100 分别标识版本号. 或者

git reset –hard 版本号 .将版本库的信息--->工作区.

Administrator@Mxb20200114 MINGW64 /c/GitRep (master)
$ git reset --hard 654c
HEAD is now at 654c826 wrote a readme.txt

Administrator@Mxb20200114 MINGW64 /c/GitRep (master)
$ git add readme.txt

Administrator@Mxb20200114 MINGW64 /c/GitRep (master)
$ git commit -m "third commit"
[master 4f76d7a] third commit
1 file changed, 2 insertions(+), 1 deletion(-)

Administrator@Mxb20200114 MINGW64 /c/GitRep (master)
$ git reflog
4f76d7a (HEAD -> master) HEAD@{0}: commit: third commit
654c826 HEAD@{1}: reset: moving to 654c
949a51f HEAD@{2}: commit: second commit
654c826 HEAD@{3}: commit (initial): wrote a readme.txt

Administrator@Mxb20200114 MINGW64 /c/GitRep (master)
$

10,git理论

Git 保存当前库中文件状态 --- add -----> stage  ----   commit  --->  版本库.

uncheck file   -------------取消暂存区.

restore file    -------------将暂存区的文件恢复到工作区

reset –hard 版本号  ------------将版本区的文件恢复到工作区.

GIt   查看命令     git    status  ---查看状态;git diff ---查看区别.

Git  log   查看当前保存的版本号

Git  reflog  查看版本操作.

11,删除文件相关操作

$ git status
On branch master
Changes not staged for commit:
(use "git add/rm …" to update what will be committed)
(use "git checkout -- …" to discard changes in working directory)

deleted:    test.txt

no changes added to commit (use "git add" and/or "git commit -a")

12, 创建SSH密钥

12.1 利用命令创建SSH,并且在Administrator文件夹下面找到SSH.PUB

Administrator@Mxb20200114 MINGW64 /c/GitRep (master)
$ ssh-keygen -t rsa -C "357670442@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):
Created directory '/c/Users/Administrator/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Administrator/.ssh/id_rsa.
Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:fBzUtP87VnN1GcGy8nDxNqgFUf6IXxhimla+nelWi1w 357670442@qq.com
The key's randomart image is:
+---[RSA 3072]----+
| .++….|
| . .o+ o |
| .+oo* o|
| . .*+o*==o|
| S+ooBooo+|
| .. .+.+Eo|
| ..=+ *|
| .+ +.|
| …..|
+----[SHA256]-----+

12.2 在GITHUB里面登陆,并且在Setting –>SSH AND GPG KEYS 里面添加 SSH kEYS.

12.3 将本地仓库GitRep 关联到远程的GitRep

Administrator@Mxb20200114 MINGW64 /c/GitRep (master) $ git remote add origin git@github.com:357670442/GitRep.gitAdministrator@Mxb20200114 MINGW64 /c/GitRep (master) $ git push -u origin master// +u 不仅推送本地分支(版本分支)而且还进行了关联. The authenticity of host 'github.com (13.250.177.223)' can't be established. RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts. Enumerating objects: 6, done. Counting objects: 100% (6/6), done. Compressing objects: 100% (4/4), done. Writing objects: 100% (6/6), 475 bytes | 67.00 KiB/s, done. Total 6 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), done. To github.com:357670442/GitRep.git  * [new branch]      master -> master Branch 'master' set up to track remote branch 'master' from 'origin'.

要关联一个远程库,使用命令git remote add origin git@server-name:path/repo-name.git

关联后,使用命令git push -u origin master第一次推送master分支的所有内容;

此后,每次本地提交后,只要有必要,就可以使用命令git push origin master推送最新修改;

13,从远程库克隆

cd c:
$ cd c:

Administrator@Mxb20200114 MINGW64 /c
$ git clone git@github.com:357670442/WindowsFormsApp2.git
Cloning into 'WindowsFormsApp2'…
remote: Enumerating objects: 52, done.
remote: Counting objects: 100% (52/52), done.
remote: Compressing objects: 100% (38/38), done.
remote: Total 52 (delta 26), reused 40 (delta 14), pack-reused 0
Receiving objects: 100% (52/52), 27.51 KiB | 163.00 KiB/s, done.
Resolving deltas: 100% (26/26), done.

地址来自于此

14,分支管理

14.1 分支理论

一开始的时候,master分支是一条线,Git用master指向最新的提交,再用HEAD指向master,就能确定当前分支,以及当前分支的提交点:

增加分支后A

此后,每一次提交就变化了B

分支合并C

分支删除D

14.2 实战

Administrator@Mxb20200114 MINGW64 /c/gitrep (master)
$ git checkout -b dev //创建并新建分支.情况A
Switched to a new branch 'dev'

Administrator@Mxb20200114 MINGW64 /c/gitrep (dev)
$ git add readme.txt
Administrator@Mxb20200114 MINGW64 /c/gitrep (dev)
$ git commit -m " dev branch"//新的提交 ,情况B
[dev 340b649] dev branch
1 file changed, 2 insertions(+), 1 deletion(-)

Administrator@Mxb20200114 MINGW64 /c/gitrep (dev)
$ git checkout master //切换到master分支,并且更改当前工作区内容.
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

Administrator@Mxb20200114 MINGW64 /c/gitrep (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

Administrator@Mxb20200114 MINGW64 /c/gitrep (master)
$ git checkout dev //更改到分支dev,变化B.
Switched to branch 'dev'

Administrator@Mxb20200114 MINGW64 /c/gitrep (dev)
$ git checkout master //切换到master分支,HEAD 指向Master.
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

Administrator@Mxb20200114 MINGW64 /c/gitrep (master)
$ git merge dev //将master--超前到dev,合并.master指向dev.
Updating 4f76d7a..340b649
Fast-forward
readme.txt | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

Administrator@Mxb20200114 MINGW64 /c/gitrep (master)
$ git branch -d dev 删除dev分支.(也就是删除dev指针).
Deleted branch dev (was 340b649).

Administrator@Mxb20200114 MINGW64 /c/gitrep (master)
$ git branch //查看当前branch.
* master

Git鼓励大量使用分支:

查看分支:git branch

创建分支:git branch <name>

切换分支:git checkout <name>或者git switch <name>

创建+切换分支:git checkout -b <name>或者git switch -c <name>

合并某分支到当前分支:git merge <name>

删除分支:git branch -d <name>

14.3 Git 合并冲突分支的方法:

Administrator@Mxb20200114 MINGW64 ~
$ cd GitRep
bash: cd: GitRep: No such file or directory

Administrator@Mxb20200114 MINGW64 ~
$ cd c:\gitrep

Administrator@Mxb20200114 MINGW64 /c/gitrep (master)
$ git branch
feature1
* master

Administrator@Mxb20200114 MINGW64 /c/gitrep (master)
$ git branch -d feature1
Deleted branch feature1 (was 4ecc3a5).

Administrator@Mxb20200114 MINGW64 /c/gitrep (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 4 commits.
(use "git push" to publish your local commits)

nothing to commit, working tree clean

Administrator@Mxb20200114 MINGW64 /c/gitrep (master)
$ git push origin master
Connection reset by 52.74.223.119 port 22
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Administrator@Mxb20200114 MINGW64 /c/gitrep (master)
$ git push origin master
Enumerating objects: 14, done.
Counting objects: 100% (14/14), done.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (12/12), 1.00 KiB | 205.00 KiB/s, done.
Total 12 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), done.
To github.com:357670442/GitRep.git
4f76d7a..8a0ff69 master -> master

Administrator@Mxb20200114 MINGW64 /c/gitrep (master)
$ $ git log --graph --pretty=oneline --abbrev-commit
bash: $: command not found

Administrator@Mxb20200114 MINGW64 /c/gitrep (master)
$ gitt log --graph
bash: gitt: command not found

Administrator@Mxb20200114 MINGW64 /c/gitrep (master)
$ git log --graph
* commit 8a0ff69d95648922252f671adcd3861a28e85f7c (HEAD -> master, origin/master)
|\ Merge: a065329 4ecc3a5
| | Author: mxb 357670442@qq.com
| | Date: Thu Jan 16 13:08:08 2020 +0800
| |
| | solve conflict
| |
| * commit 4ecc3a51b9b93de7092c54d67e7a52b643e5f804
| | Author: mxb 357670442@qq.com
| | Date: Thu Jan 16 13:00:32 2020 +0800
| |
| | and simple
| |
* commit 8a0ff69d95648922252f671adcd3861a28e85f7c (HEAD -> master, origin/mast
er)
|\ Merge: a065329 4ecc3a5
| | Author: mxb 357670442@qq.com
| | Date: Thu Jan 16 13:08:08 2020 +0800
| |
| | solve conflict
| |
| * commit 4ecc3a51b9b93de7092c54d67e7a52b643e5f804
| | Author: mxb 357670442@qq.com
| | Date: Thu Jan 16 13:00:32 2020 +0800
| |
| | and simple
| |
* commit 8a0ff69d95648922252f671adcd3861a28e85f7c (HEAD -> master, origin/mast
er)
|\ Merge: a065329 4ecc3a5
| | Author: mxb 357670442@qq.com
| | Date: Thu Jan 16 13:08:08 2020 +0800
| |
| | solve conflict
| |
| * commit 4ecc3a51b9b93de7092c54d67e7a52b643e5f804
| | Author: mxb 357670442@qq.com
| | Date: Thu Jan 16 13:00:32 2020 +0800
| |
| | and simple
| |
* | commit a065329eafd6ac667bea17148127e9dbd9934be2
|/ Author: mxb 357670442@qq.com
| Date: Thu Jan 16 13:02:35 2020 +0800
|
| & simple
|
* commit 340b6493a4d0be9f05fa3f8c90b01ca50ed62c5c
| Author: mxb 357670442@qq.com
| Date: Thu Jan 16 12:48:23 2020 +0800
|
| dev branch
|
* commit 4f76d7a02747fb356b7afca52347eb8687f41331
| Author: mxb 357670442@qq.com
| Date: Thu Jan 16 09:23:23 2020 +0800
|
| third commit
|
* commit 654c826349e8689059b65b77a4daac76ec98fcd3
Author: mxb 357670442@qq.com
Date: Thu Jan 16 08:59:47 2020 +0800

  wrote a readme.txt

Administrator@Mxb20200114 MINGW64 /c/gitrep (master)

Git解决冲突的方法,在回到Master之后,进行Merge,然后其再上一个版本,这个时候,手动进行文件更改,然后

再次进行合并.用`git log --graph`命令可以看到分支合并图。按q退出.

14.4分支管理策略

使用 $ git merge --no-ff -m "merge with no-ff" dev

新建一个commit,然后进行合并.

删除未合并的分支:

git branch -D 强行删除

标签的使用方法

Administrator@Mxb20200114 MINGW64 /c/gitrep (master)
$ git tag v0.9 340b64

Administrator@Mxb20200114 MINGW64 /c/gitrep (master)
$ git tag
v0.9
v1.0

Administrator@Mxb20200114 MINGW64 /c/gitrep (master)
$ git show v0.9
commit 340b6493a4d0be9f05fa3f8c90b01ca50ed62c5c (tag: v0.9)
Author: mxb 357670442@qq.com
Date: Thu Jan 16 12:48:23 2020 +0800

 dev branch

diff --git a/readme.txt b/readme.txt
index b3dd77f..1cede17 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,3 +1,4 @@

  1. 命令git tag <tagname>用于新建一个标签,默认为HEAD,也可以指定一个commit id;

  2. 命令git tag -a <tagname> -m "blablabla..."可以指定标签信息;

  3. 命令git tag可以查看所有标签。

  4. 命令 git show 可以查询信息.

15 码云的使用

注册码云账号,添加SSH,参考前面.然后进行仓库的添加.

Administrator@Mxb20200114 MINGW64 /c/gitrep (master)
$ git remote add gitee_origin https://gitee.com/mao_qin_bin/GitRep.git

Administrator@Mxb20200114 MINGW64 /c/gitrep (master)
$ git push -u gitee_origin master
Enumerating objects: 18, done.
Counting objects: 100% (18/18), done.
Compressing objects: 100% (12/12), done.
Writing objects: 100% (18/18), 1.38 KiB | 83.00 KiB/s, done.
Total 18 (delta 5), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-3.8]
To https://gitee.com/mao_qin_bin/GitRep.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'gitee_origin'.

Administrator@Mxb20200114 MINGW64 /c/gitrep (master)
$

`

远程的操作:

远程仓库查看  git remote –v

远程慷慨添加 git remote add origin https://gitee.com/mao_qin_bin/GitRep.git

`

远程仓库删除 git remote rm origin