git 使用

16 篇文章 1 订阅
2 篇文章 0 订阅

一、git 安装

安装完只要在桌面右键点击能出来这个就行,点 Git Bash 这个选项弹出一个命令窗口,就说明安装成功了

在这里插入图片描述

二、git 基本操作

1、git 仓库全局配置

安装完成之后设置全局机器信息,所有 git 仓库都会使用这个配置

$ git config --global user.name "userName"
$ git config --global user.email "email@example.com"

2、创建一个空目录

$ mkdir gitTest # 创建文件夹 gitTest
$ cd gitTest	# 切换到 gitTest 下
$ pwd			# 查看当前目录
/d/gitTest

3、初始化 git 仓库

$ git init # 初始化命令
Initialized empty Git repository in D:/gitTest/.git/

初始化后文件夹内会多出一个 .git 目录,并且自动创建一个 master 分支

在这里插入图片描述

4、添加一个文件到本地仓库

先创建一个文件,然后添加文件到仓库中

git add 添加命令

$ git add test.java

5、提交文件到本地仓库

git commit 提交命令

$ git commit -m "init project"
[master (root-commit) 3443f03] init project
 1 file changed, 5 insertions(+)
 create mode 100644 test.java

-m 后面是提交的说明,提交成功后会显示更改的文件(file changed)和更改的内容(insertions)

6、修改提交的备注信息

$ git commit --amend

可以直接修改刚提交的备注信息

init project

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Tue Nov 23 10:36:13 2021 +0800
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
#       new file:   test.java

修改完成后

$ git commit --amend
[master 1a87d51] init project 1
 Date: Tue Nov 23 10:36:13 2021 +0800
 1 file changed, 5 insertions(+)
 create mode 100644 test.java

7、修改文件并提交

修改文件后重新添加并提交文件,提交会生成一个快照记录下来

$ git add test.java

$ git commit -m "append"
[master 8fb6416] append
 1 file changed, 1 insertion(+)

8、查看历史记录

git log 显示最近到最远的提交日志,提交的版本号是个 16进制数

– graph:分支合并图

– abbrev-commit:提交版本号缩略

–pretty=oneline 减少输出的信息

Author: userName <email@example.com>
Date:   Tue Nov 23 14:16:23 2021 +0800

    append

commit 384556a202ac92f3787033369d2f8645a16021d3
Author: userName <email@example.com>
Date:   Tue Nov 23 10:36:13 2021 +0800

    init project

HEAD 表示当前版本,也就是最新的提交

$ git log --pretty=oneline
8fb6416efee0387992c4045e366aee735dd6df0f (HEAD -> master) append
384556a202ac92f3787033369d2f8645a16021d3 init project

9、回退历史版本

git reset 回退版本,上一个版本就是 HEAD^,往上几个版本可以写成 HEAD^n

$ git reset HEAD^ # 回退上一个版本
Unstaged changes after reset:
M       test.java
$ git log --pretty=oneline # 查看历史版本
384556a202ac92f3787033369d2f8645a16021d3 (HEAD -> master) init project

已经成功回退了版本

10、查看历史命令

如果想要重新回退之前的版本,我们需要使用版本号,如果我们忘记的话可以用 git reflog 来查看历史命令

$ git reflog
b00a361 (HEAD -> master) HEAD@{0}: reset: moving to b00a361
04c6b2c HEAD@{1}: commit: append
b00a361 (HEAD -> master) HEAD@{2}: commit (initial): init project

然后使用版本号进行回退

$ git reset 04c6b2c
Unstaged changes after reset:
M       test.java

11、查看状态

使用 git status 查看状态

$ 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:   test.java

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

三、暂存区

git add 是把需要提交的文件添加到暂存区

git commit 是把暂存区的内容提交到当前分支

增加一个文件后使用 git status 看一下状态

$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test.class

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

增加的这个文件 test.class 还没有被添加到暂存区,它的状态是 Untracked

重新提交后查看状态

$ git status
On branch master
nothing to commit, working tree clean

没有其它修改的话,没有需要提交的内容工作区很干净

四、修改文件

1、修改文件内容

git commit 只会提交已经存到暂存区的内容,每次修改后想要把代码提交到分支,都需要先 git add 然后 git commit,多次修改需要多次 git add

2、查看本地代码和已经提交代码的区别

git diff HEAD – file

$ git diff HEAD -- test.java
diff --git a/test.java b/test.java
index 6b79563..075b2d8 100644
--- a/test.java
+++ b/test.java
@@ -1,6 +1,5 @@
 public class test{
     public static void main(String[] args) {
-
         System.out.println("Hello Word");
     }
 }

3、撤销修改

把暂存区的修改撤销,git reset HEAD file

$ git reset HEAD test.java
Unstaged changes after reset:
M       test.java

4、删除文件

从本地仓库库里面删除

$ git rm test.java
rm 'test.java'

$ git diff HEAD -- test.java # 删除后本地代码跟仓库代码区别
diff --git a/test.java b/test.java
deleted file mode 100644
index 6b79563..0000000
--- a/test.java
+++ /dev/null
@@ -1,6 +0,0 @@
-public class test{
-    public static void main(String[] args) {
-
-        System.out.println("Hello Word");
-    }
-}

删除后提交

$ git commit -m "remove test.java"
[master d7b4e49] remove test.java
 1 file changed, 6 deletions(-)
 delete mode 100644 test.java

五、分支操作

1、创建分支

git branch name 创建分支

git checkout -b name 创建并切换分支

$ git branch dev # 创建分支 dev
$ git checkout dev # 切换分支到 dev
Switched to branch 'dev'

$ git checkout -b dev # 相当于上面两条命令
Switched to branch 'dev'

2、查看当前分支

git branch

$ git branch
* dev
  master

3、合并分支

git merge 合并指定分支到当前分支

$ git merge dev
Already up to date.

4、删除分支

git branch -d name 删除分支

$ git branch -d dev
Deleted branch dev (was 4c5ebec).
$ git branch
* master

5、解决冲突

当两个分支都对同一个文件做了修改,我们无法快速合并,只能把修改的部分发生冲突的地方解决后进行合并

切换到 master 分支后合并 feature 分支

$ git merge feature
CONFLICT (add/add): Merge conflict in 新建文本文档.txt
Auto-merging 新建文本文档.txt
Automatic merge failed; fix conflicts and then commit the result.

使用 git status 查看冲突的文件

$ git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
        both added:      "\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt"
        
        no changes added to commit (use "git add" and/or "git commit -a")

冲突文件的内容

<<<<< HEAD 当前分支内容,>>>>> 要合并分支内容,中级用 =====间隔

<<<<<<< HEAD
111
=======
222
>>>>>>> feature

修改后进行保存,然后提交

$ git add 新建文本文档.txt
$ git commit -m "merge feature"
[master a664482] merge feature

查看分支合并情况

$ git log --graph --pretty=oneline --abbrev-commit
*   a664482 (HEAD -> master) merge feature
|\
| * 5b4caaf (feature) txt
* | 00b1678 txt
|/
* 4c5ebec add test.class
* 1087494 append2
* 04c6b2c append
* b00a361 init project

6、非快速合并

一般默认合并分支使用 fast forward 快速合并,快速合并删除分支后,会丢失分支信息

使用 --no-ff 参数进行普通合并,分支历史上会有分支信息

$ git merge --no-ff -m "merge feature no-ff" feature

7、git stash

使用 git stash 暂时保存一下我们工作区更改的内容

$ git stash
Saved working directory and index state WIP on master: 8b15ff3 delete test
$ git status
On branch master
nothing to commit, working tree clean

查看 stash 列表

$ git stash list
stash@{0}: WIP on master: 8b15ff3 delete test

恢复 stash

$ git stash apply stash@{0}
Removing 新建文本文档.txt
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    "\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt"

恢复的同时并不会删除已经保存的 stash

删除 stash

$ git stash drop stash@{0}

恢复并删除 stash

使用 git stash pop

$ git stash pop stash@{0}
Removing 新建文本文档.txt
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    "\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt"

no changes added to commit (use "git add" and/or "git commit -a")
Dropped stash@{0} (5130aa6b40794e0099400aae6d2ee7d6ca91bb9c)

六、远程仓库

1、设置本地分支和远程分支链接

$ git branch --set-upstream-to=origin/master master

2、查看远程库信息

$ git remote -v

3、推送分支

$ git push origin master

4、拉取分支

git pull
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一起来搬砖呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值