Git学习笔记

Git学习笔记

1.新建仓库

E:\git>git init
Initialized empty Git repository in E:/git/.git/

E:\git>

image

2.添加文件到暂存区

随便创建文件,写句话,然后使用命令即可。多个文件,以空格分开即可。

比如: git add a.txt b.txt c.txt

E:\git>git add first.txt

E:\git>

添加成功,不会有什么提示

3.提交文件到仓库

image

提交成功,会有提示。

image

当然是可以选择只提交暂存区内的一部分文件。

紧接着试一下不带-m提交文件

E:\git>git commit                                                                                                       [master 2482eb4] hello world this is b.txt
 1 file changed, 1 insertion(+)
 create mode 100644 b.txt

不加-m的话,就会产生一个类似于vim打开的文件窗口,让你输入提交信息。

4.查看状态

E:\git>git status
On branch master
nothing to commit, working tree clean

新建c.txt,但是不加到暂存区,再看状态。

E:\git>git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        c.txt

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

它会给出建议,让你添加到暂存区。

E:\git>git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   c.txt


E:\git>

提交以后,再看状态,会提示你需要提交。提交以后,就和最开始一样了。

这里有个建议命令是git restore --staged c.txt。

输入它的话,git就会放弃跟踪c.txt文件。也就是撤销了之前的add操作。(可看到操作前后文件的图标变化)

5.restore命令

主要用于工作目录和暂存区不一致的情况。

比如提交到暂存区,但是没有提交到仓库,而是在工作目录修改了刚才提交的文件。

E:\git>git add e.txt

E:\git>git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   e.txt

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:   e.txt


E:\git>

类似于这样的,先git add e.txt,然后修改e.txt,再查看状态。

也就是撤销了git add 操作。

一步到位进行添加到暂存区和仓库的操作。

对提交到仓库过的e.txt,进行修改。

E:\git>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:   e.txt

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

E:\git>git commit -am "修改 e.txt"
[master c460f01] 修改 e.txt
 1 file changed, 3 insertions(+), 1 deletion(-)

E:\git>

然后一步到位的提交到仓库。

6.查看历史提交

git log
git reflog

git reflog看得更多

7.版本回退(reset)

把仓库的文件,还原到暂存区。

这个挺复杂的。

这里已经两次提交过e.txt了。

git reset head~~~ = git reset head~3 简写

image

使用git reset head~,把HEAD移动到上次提交之前。简单来说。

上次使用了这个命令一步到位。

git commit -am "修改 e.txt"

现在移动了head指针,就变成了

啥也没有了,即没有add,也没有commit了

把上次的commit给去掉了。使用git log也看不见了,但是git reflog还是有记录的。

此外,需要注意的是,git reset 不加修饰词就等效于git reset --mixed,也就是git reset默认是mixed

还要其他修饰词:soft

从新一步到位的提交e.txt,然后测试这个命令。

image

效果,感觉就是撤销了commit命令一样,只是把修改内容提交到了暂存区

最后一个修饰词:hard

image

来来回回,反复拉扯,刚才也只是第二次提交e.txt。这次使用hard修饰,这个仓库就回到了第一次提交e.txt的状态,后面的修改都无了。

当然,还是可以再回到之前的。

E:\git>git reset c460f01
Unstaged changes after reset:
M       e.txt

E:\git>git reset --hard b19a9e1
HEAD is now at b19a9e1 add e.txt again

使用git reflog查看之前所有的版本快照,然后选择性的回滚即可。reset后面直接接快照ID即可。

8.文件对比(git diff)

E:\git>git diff e.txt
diff --git a/e.txt b/e.txt
index 25787ec..b2359db 100644
--- a/e.txt
+++ b/e.txt
@@ -1,3 +1,4 @@
 e.txt add some words
 second line
-third line
\ No newline at end of file
+third line
+fourth line.
\ No newline at end of file

E:\git>

如果差异很大,要具体查看,就类似于vim里面的操作了。

git diff 快照ID1 快照ID2,即可比较快照之间的不同
 git diff --cached 比较最新快照和暂存区的不同

9.修改提交

1.单纯的修改最后提交的消息

git commit --amend -m "modify last commit words"

2.最后一次提交漏掉了提交文件,重新一起提交(没啥实际意义吧,只是单纯git log的时候修改了最后一次提交的信息)

修改两个文件进行测试,但是commit只提交一个。

image

效果嘛,就像

git reset --soft head~
git commit -m "xxxx"

10.删除文件

E:\git>git rm e.txt
rm 'e.txt'

E:\git>

连回收站都找不到的,当然,版本回退啥的肯定还有。

E:\git>git rm e.txt
rm 'e.txt'

E:\git>git commit -m "a message"
[master c60a117] a message
 1 file changed, 5 deletions(-)
 delete mode 100644 e.txt

E:\git>git status
On branch master
nothing to commit, working tree clean

E:\git>

删除文件后,再次提交,就能clean这个版本控制树了。

11.重命名文件

image

其实和linux下的一些常见命令是一样的,只是前面加上了git而已。

此外,注意mv的过程吧。

E:\git>git mv e.txt 1.txt

E:\git>git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        renamed:    e.txt -> 1.txt


E:\git>git restore --staged 1.txt

E:\git>git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    e.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        1.txt

12.让git忽略跟踪指定后缀文件。

1.创建一个 .gitignore 文件

里面添加一些过滤字符串吧,可参考这个。

https://github.com/github/gitignore/blob/main/JBoss.gitignore

13.创建分支

git branch name

image

git log --decorate --oneline

14.切换分支

E:\git>git checkout tree1
Switched to branch 'tree1'

E:\git>git status
On branch tree1
nothing to commit, working tree clean

E:\git>

切换以后,分支就成了tree1了

此后,不切换回去的话,所有修改都是对这个分支的了。

做个测试,修改1.txt,然后提交,再查看日志

image

可见最新的修改只是对这个tree1分支的。

再切换会master分支,工作目录也变成了之前的样子。

切换到master,只修改,不提交到仓库的话,就不能切换分区了。

E:\git>git checkout master
Switched to branch 'master'

E:\git>git checkout tree1
error: Your local changes to the following files would be overwritten by checkout:
        1.txt
Please commit your changes or stash them before you switch branches.
Aborting

E:\git>git add 1.txt

E:\git>git checkout tree1
error: Your local changes to the following files would be overwritten by checkout:
        1.txt
Please commit your changes or stash them before you switch branches.
Aborting

E:\git>

然后再图形化对比两个分支。

image

15.合并分支

E:\git>git merge tree1
Auto-merging 1.txt
CONFLICT (content): Merge conflict in 1.txt
Automatic merge failed; fix conflicts and then commit the result.

E:\git>

因为master和tree1分支的1.txt不同,所以会有冲突

image

再使用git status,也会有提示。

再看冲突的文件,已经被git处理过了。

image

此时需要手动处理冲突的文件,然后再保存,提交即可。

E:\git>git add 1.txt

E:\git>git commit -m "hebing"
[master a98b7cd] hebing

E:\git>git status
On branch master
nothing to commit, working tree clean

image

可以看见,解决冲突提交后,分支也变化了。

测试:

E:\git>git checkout -b tree2
Switched to a new branch 'tree2'

E:\git>git status
On branch tree2
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        2.txt

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

E:\git>git add 2.txt

E:\git>git commit -m "tree2's 2.txt"
[tree2 c0de2a7] tree2's 2.txt
 1 file changed, 1 insertion(+)
 create mode 100644 2.txt

E:\git>

新建并切换一个分支,新建一个文件并提交。

image

可以看见tree2和master的区别。

切换会master,再合并

git checkout master
E:\git>git merge tree2
Merge made by the 'recursive' strategy.
 2.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 2.txt

E:\git>

这是没有冲突,合并直接就完成了。

此时,tree1已经落后了,可以更新一下:

E:\git>git checkout tree1
Switched to branch 'tree1'

E:\git>git merge master
Updating bdfb523..7767b17
Fast-forward
 1.txt | 3 ++-
 2.txt | 1 +
 2 files changed, 3 insertions(+), 1 deletion(-)
 create mode 100644 2.txt

E:\git>

可见,git merge xxx,就是让xxx和现在这个分支合并

16.删除分支

删除分支前后,其他图形没有什么大的变化,只是分支的名字消失了。
E:\git>git log --decorate --all --graph --oneline
* bf50897 (HEAD -> master, tree1) aa.txt
*   7767b17 Merge branch 'tree2'
|\
| * c0de2a7 (tree2) tree2's 2.txt
* |   a98b7cd hebing
|\ \
| |/
| * bdfb523 modify 1.txt in tree1
* | 206aef7 master's 1.txt
|/
* 3b1a8ea 1.txt
* fd68a01 no problem
* 35304e2 both
* fe639aa modify last commit words
* b19a9e1 add e.txt again
* 3d5775f add e.txt
* c0e4f20 d.txt
* cccf509 c.txt
* 2482eb4 hello world this is b.txt
* bd3ed1e only commit a.txt
* 961f947 add first.txt

E:\git>git branch -d tree2
Deleted branch tree2 (was c0de2a7).

E:\git>git log --decorate --all --graph --oneline
* bf50897 (HEAD -> master, tree1) aa.txt
*   7767b17 Merge branch 'tree2'
|\
| * c0de2a7 tree2's 2.txt
* |   a98b7cd hebing
|\ \
| |/
| * bdfb523 modify 1.txt in tree1
* | 206aef7 master's 1.txt
|/
* 3b1a8ea 1.txt
* fd68a01 no problem
* 35304e2 both
* fe639aa modify last commit words
* b19a9e1 add e.txt again
* 3d5775f add e.txt
* c0e4f20 d.txt
* cccf509 c.txt
* 2482eb4 hello world this is b.txt
* bd3ed1e only commit a.txt
* 961f947 add first.txt

17.匿名分支

E:\git>git checkout head~
Note: switching to 'head~'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 7767b17 Merge branch 'tree2'

E:\git>

checkout后面当然也是可以像之前reset一样,后面是head~或者快照ID之类的。

E:\git>git checkout bf50897
Previous HEAD position was 7767b17 Merge branch 'tree2'
HEAD is now at bf50897 aa.txt

E:\git>git status
HEAD detached at bf50897
nothing to commit, working tree clean

E:\git>
E:\git>git checkout master
Warning: you are leaving 2 commits behind, not connected to
any of your branches:

  9a5f8bf aaa.txt
  d021933 only d.txt

If you want to keep them by creating a new branch, this may be a good time
to do so with:

 git branch <new-branch-name> 9a5f8bf

Switched to branch 'master'

E:\git>git branch noname 9a5f8bf

没啥好说的,也就熟悉下git命令。

在这个匿名分支可以随意提交,等切换回有名分支的时候,就会提示你赶紧创建分支了。

image

创建以后,就这样了。所以,我们完全可以再恢复之前删除的tree2分支,只需要给它一个名字即可。

18.checkout恢复文件。

上一个主要是分支切换的效果。

E:\git>git status
On branch master
nothing to commit, working tree clean

E:\git>git add first.txt

E:\git>git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   first.txt

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:   first.txt


E:\git>

在提交到暂存区之后,再次修改文件,就会让同一文件处于两个版本。

E:\git>git checkout first.txt
Updated 1 path from the index

此时,使用checkout 文件名,就会让工作目录的文件被暂存区的覆盖了。也就是回到了修改1次的状态。

E:\git>git checkout head first.txt
Updated 1 path from 6b781c0

还可以直接从仓库里面恢复first.txt,这下暂存区和工作目录都同步成了仓库的版本

不用测试,感觉head/branchname/快照ID 都是可以在这个地方使用的。

19.克隆远程仓库

1.使用https
git clone https://gitee.com/xxxx/test.git

使用https克隆的仓库,要提交回去,简单的git push即可。

2.使用ssh

image

在打开的控制台下。

$ ssh-keygen -t rsa -C "随便写点什么都可以"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/yyjeqhc/.ssh/id_rsa):

可以自己输入文件名,直接回车就默认是id_rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/yyjeqhc/.ssh/id_rsa):
Created directory '/c/Users/yyjeqhc/.ssh'.
Enter passphrase (empty for no passphrase):

可以直接回车,也可以自己输入私钥(也是密码)

简单点,直接一路回车,完事儿以后 输入命令,就能看见.ssh下面有东西了。

$ ls ~/.ssh/
id_rsa  id_rsa.pub

这个时候,就需要把id_rsa.pub里面的文本内容,复制到git仓库里面

以gitee为例。

image

点击管理。

image

点击部署公钥管理

image

点击公钥管理,会看见一些提示内容:

image

这样部署的公钥只能克隆和拉取,不能在本地对仓库修改后,利用ssh推送(push)到仓库,也就是提交修改

点击添加公钥

image

此时,就可以在文件夹下使用ssh克隆仓库了。

git clone git@gitee.com:xxxx/test.git

可自行对比使用https和ssh克隆命令的区别

$ git push
[session-bf4658a2] Auth error: DeployKey does not support push code
fatal: Could not read from remote repository.

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

这就是上面公钥的缺点了,不能提交回去。

image

点击添加个人公钥。

image

这个和刚才一样的添加即可。需要重新生成,注意文件名想对应即可;这个个人公钥就很好了,可以克隆,也可以推送回去。

$ git push
Enter passphrase for key '/c/Users/yyjeqhc/.ssh/id_rsa':
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 262 bytes | 262.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.3]

使用ssh,对仓库的操作,基本都需要上面输入的私钥(密码)

20.处理Pull requests

这个就是前面的合并分支了。如果是自己创建的两个不同的分支进行合并,自己根据网页提示操作即可。

简单举例:

image

image

main和tree分支,最主要的区别就是tree里面的tree.txt比main的tree.txt多了一行而已。

所以可以添加pull request请求,然后合并tree到main里面。

image

点击新建Pull Requests

image

随便填写一些内容。

image

就可以激活下面的两个按钮了。草稿的意思就不用解释了。

这里直接点击创建 Pull Request

image

这里只是测试功能而已,点击审查通过和测试通过即可。

都点击以后,即可出现合并的绿色按钮。

image

点击合并,就能把tree.txt的修改合并到main里面了。

image

其它的没试过,这里点击合并分支记即可。

image

最后还要输入一些内容,类似于评委评论的意思吧。再点击接受Pull Request即可合并完成了。

21.Git pull命令(拉取远程仓库并和本地合并)

git pull https://gitee.com/xxxx/test.git tree
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://gitee.com/yyjeqhc/test
 * branch            tree       -> FETCH_HEAD
Auto-merging tree.txt
CONFLICT (content): Merge conflict in tree.txt
Automatic merge failed; fix conflicts and then commit the result.

image

这是因为gitee上的tree分支和本地的分支出现了冲突,自己手动合并内容即可。

可以参考博客:https://www.cnblogs.com/wbl001/p/11495110.html

git pull addr branchName 

也就是从远程拉取branchName分支和本地现在状态的分支(使用git status查看)

如果branchName是主分支的话,可以省略。

22.关联远程仓库

git remote add 仓库名称 项目的地址(也就是仓库里面克隆需要的地址:https/ssh)

仓库名称,默认在第一次clone的时候设置为origin

还可用 git remote -v查看关联详情。

添加关联仓库,就可以提交代码到其他远程仓库,只需要git push/pull 远程仓库地址即可。

默认省略,就是origin那个地址。

23.提交代码到别人仓库。

1.fork别人的仓库。

这个只能在网页上别人的项目页面点击fork。

2.git clone fork出来的仓库地址。

也就是变成了clone你自己的项目地址

3.修改项目,进行commit
4.创建并切换分支
git checkout -b "newBranch"

一步到位即可。

毕竟是要提交给别人,最好是换个分支。

5.推送到你的仓库
git push origin newBranch

因为远程仓库还没有这个分支。当然了,你可以直接git push,然后根据系统提示进行操作。

6.网页上打开仓库,添加一个Pull Request即可。只是可以选择从你的分支合并到别人的分支。

然后审核也变成了别人的工作。

24.下载远程仓库的一部分文件

参考博客:https://blog.csdn.net/qq_35616850/article/details/82289568

'childfile'替换为自己要下载的文件夹名称
1. git init test && cd test     //新建仓库并进入文件夹
2. git config core.sparsecheckout true //设置允许克隆子目录
3. echo 'childfile*' >> .git/info/sparse-checkout //设置要克隆的仓库的子目录路径   //空格别漏 
4. git remote add origin git@github.com:mygithub/test.git  //这里换成你要克隆的项目和库
5. git pull origin master    //下载
posted @ 2022-05-25 23:46  念秋  阅读(185)  评论(0编辑  收藏  举报