Git常用命令

读完本文,你将了解git的常用操作。

注:本文不介绍git历史,也不介绍如何安装git,更不介绍如何使用github。

有需要的读者可以参考 同时推送代码至github和gitee

Ⅰ. 基本操作

创建版本库

mkdir git

cd git

git init
Initialized empty Git repository in /home/incipe/Desktop/git/.git/

ls -a
.  ..  .git

添加到版本库

touch first.md

echo "Hello git" > first.md

cat first.md 
 Hello git
 
git add first.md

git commit -m "first commit"
 [master (root-commit) ff57502] first commit
  1 file changed, 1 insertion(+)
  create mode 100644 first.md

git commit -m 后输入本次的提交信息,可以输入任何内容,最好是有意义的。

查看版本记录。

git log # git log --graph --pretty=oneline
commit ff57502578c70c7fc5225e1fc5aa0b6dfe6d86fc (HEAD -> master)
Author: Incipe-win <xxxxxx@163.com>
Date:   Thu Oct 1 19:50:09 2020 +0800

    first commit

版本回退

我们在 first.md 增加如下内容:

cat first.md 
Hello git
That's the second line

再次使用 git commit -m 提交。

git add first.md

git commit first.md
 [master f732109] second commit
 1 file changed, 1 insertion(+)
 
git log
commit f732109700ff998f243b67fef59976bf0c7c7ace (HEAD -> master)
Author: Incipe-win <xxxx@163.com>
Date:   Thu Oct 1 20:05:53 2020 +0800

    second commit

commit ff57502578c70c7fc5225e1fc5aa0b6dfe6d86fc
Author: Incipe-win <xxxx@163.com>
Date:   Thu Oct 1 19:50:09 2020 +0800

    first commit

回退到第一次提交时的内容:

git reset --hard HEAD^
HEAD is now at ff57502 first commit

cat first.md
Hello git

这个过程是这样的,有一个 HEAD 指针指向最新提交的那个版本,^ 表示回到上一个版本,^^ 表示上上个,有多少个 ^ 就表示回退到第几个版本,也可以用数字代替,比如:git reset --hard HEAD~1 后面这个数字表示回到第几个版本。

后面还可以接版本序列号。

如果,此时我想回到版本二应该怎么做,git rest --hard f732109700ff998f243b67fef59976bf0c7c7ace ,这串字符就是版本序列号,可以不要复制全部,只复制一部分也是可以的,跟这个命令等价 git rest --hard f732109

如果找不到版本序列号应该怎么回到版本二呢?

git reflog
ff57502 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
f732109 HEAD@{1}: commit: second commit
ff57502 (HEAD -> master) HEAD@{2}: reset: moving to HEAD
ff57502 (HEAD -> master) HEAD@{3}: reset: moving to HEAD
ff57502 (HEAD -> master) HEAD@{4}: commit (initial): first commit

git reset --hard f732109     
HEAD is now at f732109 second commit

cat first.md 
Hello git
That's the second line

git reflog 查看所有的操作记录。

每次提交,回退都是 HEAD 指针的修改。

工作区和暂存区

查看当前工作树状态, git status

cat first.md 
Hello git

# 给first.md增加如下内容,但不提交
cat first.md 
Hello git
Where there is a will, there is a way

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

no changes added to commit (use "git add" and/or "git commit -a")
# 这里会给个提示,告诉我修改了fitst.md 内容,但是没有提交。

git add first.md
git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   first.md
	
git commit -m "version 3"
[master c6ea42a] version 3
 1 file changed, 1 insertion(+)
 
git status
On branch master
nothing to commit, working tree clean

总的来讲,我们操作的目录就叫工作区,git add 就是把工作区的文件体检到暂存区, git commit -m 就是把暂存区所有的修改记录创建一个版本记录。

撤销修改

cat first.md 
Hello git
Where there is a will, there is a way

cat first.md 
Hello git
Where there is a will, there is a way
This is the third line

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

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

git restore first.md 

cat first.md 
Hello git
Where there is a will, there is a way

文件对比

ls -lart 
total 20
drwxr-xr-x 3 incipe incipe 4096 10月  1 19:44 ..
-rw-rw-r-- 1 incipe incipe   48 10月  1 21:07 first.md
-rw-rw-r-- 1 incipe incipe   66 10月  1 21:10 second.md
drwxrwxr-x 3 incipe incipe 4096 10月  1 21:10 .
drwxrwxr-x 8 incipe incipe 4096 10月  1 21:10 .git

cat first.md 
Hello git
Where there is a will, there is a way

cat second.md 
Hello git
Where there is a will, there is a way
This is new line

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:   second.md

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

git diff HEAD -- second.md
diff --git a/second.md b/second.md
index f9c6e01..6e93719 100644
--- a/second.md
+++ b/second.md
@@ -1,3 +1,4 @@
 Hello git
 Where there is a will, there is a way
+This is new line

git diff HEAD HEAD^ -- second.md
diff --git a/second.md b/second.md
deleted file mode 100644
index f9c6e01..0000000
--- a/second.md
+++ /dev/null
@@ -1,3 +0,0 @@
-Hello git
-Where there is a will, there is a way
-

每个版本的文件前面有 + - 两个符号,相应的,文件内容出现那个符号,就表示是哪个文件说拥有的,如果没有,就代表是都有的。

Ⅱ. 分支操作

创建分支

git checkout -b dev
Switched to a new branch 'dev'
# <=>
# git branch dev
# git checkout dev

git branch
* dev
  master

合并分支,git 默认会采用快速合并的方法,如果不想采用的话,可以使用 git merge --no-ff -m "合并信息" 分支

# 切换回master
git checkout master
M	second.md
Switched to branch 'master'

git merge dev
Already up to date.

git branch -d dev
Deleted branch dev (was 24df10d).

git branch
* master

分支冲突

比如下图,在 dev 分支上对某个文件进行了修改并且进行了提交,切换回 master 分支又对这个文件进行修改并且进行了提交,然后合并分支会导致冲突。

手动解决冲突即可。这个时候合并分支会出现如下信息:

Auto-merging second.md
CONFLICT (content): Merge conflict in second.md
Automatic merge failed; fix conflicts and then commit the result.

vim second.md
Hello git
Where there is a will, there is a way
<<<<<<< HEAD
This is a new line
=======
This is third line
>>>>>>> dev

# 修改为
Hello git
Where there is a will, there is a way
This is a new line
This is third line

# 手动提交
git add second.md
git commit -m "解决冲突"
[master b75d71c] 解决冲突

git log --graph --pretty=oneline
*   b75d71c42798ca48cf869cf6a0c3709c4dc8127e (HEAD -> master) 解决冲突
|\  
| * f0bc35a5b50666bf709a0c510f7ab73f81d4cdb1 (dev) dev
* | 51d0b83dd8be9b3593f1effaaab82f54dd7b636c master
|/  
* 6f2fc2d87aa23e9a5e82746497f7001c7a0e36ea update

如果是这种情况,在 dev 分支上,建立了新的文件,进行提交,切换回 master 分支在创建的文件夹里面进行添加,最后合并分支,不会报错,git 会手动帮我们进行一次新的提交。

Ⅲ. 总结

git 用法挺多的,这里罗列了一部分常用的东西,记录下来,方便日后查找用法直接定位。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值