Git日常命令以及使用案例

目前市面上流行使用的项目版本管理工具有SVN和Git两种;下面看两种工具的定义:

SVN是subversion的缩写,是一个开放源代码的版本控制系统,通过采用分支管理系统的高效管理,简而言之就是用于多个人共同开发同一个项目,实现共享资源,实现最终集中式的管理。

优点: SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以首先要从中央服务器哪里得到最新的版本,然后干活,干完后,需要把自己做完的活推送到中央服务器

缺点: 必须联网才能工作,如果在局域网还可以,带宽够大,速度够快。

Git(读音为/gɪt/)是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。它就没有中央服务器的,每个人的电脑就是一个完整的版本库。

基本命令操作演示:

0:查看git版本
$ git version
git version 2.20.1.windows.1

1:创建git仓库 
$ git init  #初始化git仓库                                                                            
Initialized empty Git repository in C:/**/pro_git/.git/  #看到这里就说明git仓库初始完成,目录中会多出.git目录
(没有的话点击查看,将隐藏的项目勾选就能看到了) 
                                                                                                
2:创建文件
$ touch a.txt #创建文件的第一种方式;创建a.txt文件                  
$ vim a.txt #编辑a.txt文件,写入内容
$ echo "1111111" > b.txt #创建文件的第二种方式;创建b.txt并将“1111111”写入到文件中;

3:查看文件在git仓库中的状态
$ git status
On branch master
No commits yet
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        a.txt
        b.txt
nothing added to commit but untracked files present (use "git add" to track)

4:将文件交给git管理
$ git add a.txt b.txt #第一种方式
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in b.txt.
The file will have its original line endings in your working directory

$ git add . #第二种方式,git add 空格. 表示将所有文件提交
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in b.txt.
The file will have its original line endings in your working directory

5:已git add文件但未commit,此时要撤回
$ git reset HEAD -- a.txt b.txt #撤回一个或多个文件
$ git reset HEAD -- #撤回add的所有文件

6:commit文件
$ git commit -m "提交a.txt和b.txt文件" #字符串为描述或注释信息
 [master (root-commit) 121c2fb] 提交a.txt和b.txt文件
 2 files changed, 2 insertions(+)
 create mode 100644 a.txt
 create mode 100644 b.txt

$ git status #commit后,工作区是干净的
On branch master
nothing to commit, working tree clean

7:查看文件内容
$ cat a.txt
aaaaaaaaaaa
$ cat b.txt
bbbbb
#通过vim命令对a.txt和b.txt文件做出修改
$ cat a.txt
aaaaaaaaaaa
a0a0a0a0a0a0
$ cat b.txt
bbbbb
b0b0b0
#执行add和commit

8:git日志和跟踪管理
$ git log #查看每次操作的日志情况。
commit c9eaa0baa53197bbdac3bfcb8c4f267f46f25739 (HEAD -> master)
Author: Azzan <lz527657138@qq.com>
Date:   Sun Apr 26 16:24:15 2020 +0800
    submit 2times

commit 013a5db73f2b184707b51f9e6dabbb79338d3076
Author: Azzan <lz527657138@qq.com>
Date:   Sun Apr 26 15:56:35 2020 +0800
    first commit

$ git log --pretty=oneline #可以一行显示,查看关键信息
c9eaa0baa53197bbdac3bfcb8c4f267f46f25739 (HEAD -> master) submit 2times
013a5db73f2b184707b51f9e6dabbb79338d3076 first commit

9:查看内容的修改,在a.txt中追加a1a1a1a1a1
$ cat a.txt
aaaaaaaaaaa
a0a0a0a0a0a0
a1a1a1a1a1

$ git diff a.txt #查看做了什么修改,带有+号的内容
diff --git a/a.txt b/a.txt
index 3da5807..d9e295e 100644
--- a/a.txt
+++ b/a.txt
@@ -1,2 +1,3 @@
 aaaaaaaaaaa
 a0a0a0a0a0a0
+a1a1a1a1a1

10:版本回退
$ git reflog a.txt #查看操作记录信息
e2c255a (HEAD -> master) HEAD@{0}: commit: xiugailefile a.txt
c9eaa0b HEAD@{1}: commit: submit 2times
013a5db HEAD@{2}: reset: moving to HEAD^
92123c5 HEAD@{3}: commit: submit file
013a5db HEAD@{4}: reset: moving to HEAD
013a5db HEAD@{5}: reset: moving to 013a5db
013a5db HEAD@{6}: commit (initial): first commit

此时,在e2c255a,要退回到版本c9eaa0b
$ git reset --hard c9eaa0b
HEAD is now at c9eaa0b submit 2times

$ git reflog a.txt #再次查看操作记录信息
c9eaa0b (HEAD -> master) HEAD@{0}: reset: moving to c9eaa0b
e2c255a HEAD@{1}: commit: xiugailefile a.txt
c9eaa0b (HEAD -> master) HEAD@{2}: commit: submit 2times
013a5db HEAD@{3}: reset: moving to HEAD^
92123c5 HEAD@{4}: commit: submit file
013a5db HEAD@{5}: reset: moving to HEAD
013a5db HEAD@{6}: reset: moving to 013a5db
013a5db HEAD@{7}: commit (initial): first commit

$ cat a.txt #查看a.txt文件内容,看到没有了a1a1a1a1a1
aaaaaaaaaaa
a0a0a0a0a0a0

此时,又想回到e2c255a怎么办?
$ git reset --hard e2c255a 版本
HEAD is now at e2c255a xiugailefile a.txt

$ cat a.txt #查看a.txt文件内容,a1a1a1a1a1又特么回来了,这时候是不是想打人O(∩_∩)O哈哈~
aaaaaaaaaaa
a0a0a0a0a0a0
a1a1a1a1a1

11:查看分支
lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (master)
$ git branch
* master

12:创建分支
$ git branch ygzy
$ git branch
* master
  ygzy

13:切换分支
$ git checkout ygzy
Switched to branch 'ygzy'
lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (ygzy)

14:创建分支并切换到最新分支一步完成(11+12+13)
lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (master)
$ git checkout -b ygzy
Switched to a new branch 'ygzy'

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (ygzy)

15:分支删除(如果要删除当前分支ygzy,请先切换到其他分支或回到主分支master,再删除)
lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (ygzy)
$ git branch -d ygzy #当前所在分支为ygzy,然后删除ygzy等于自杀,所以删除分支失败
error: Cannot delete branch 'ygzy' checked out at 'C:/Users/lenovo/Desktop/git_command/pro_git'

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (ygzy)
$ git checkout master #切换到主分支
Switched to branch 'master'

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (master)
$ git branch -d ygzy #删除分支ygzy
Deleted branch ygzy (was e2c255a).

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (master)
$ git branch #查看分支
* master

16:合并分支(解决冲突)
lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (master)
$ touch c.txt # 创建c.txt

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (master)
$ vim c.txt #编辑c.txt

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (master)
$ cat c.txt #查看c.txt文件的内容
ccc

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (master)
$ git add . #添加到暂存区
warning: LF will be replaced by CRLF in c.txt.
The file will have its original line endings in your working directory

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (master)
$ git commit -m "create c.txt" #提交到版本库
[master (root-commit) 4fa23bf] create c.txt
 1 file changed, 1 insertion(+)
 create mode 100644 c.txt

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (master)
$ git checkout -b ygzy #创建分支ygzy 并切换到ygzy
Switched to a new branch 'ygzy'

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (ygzy)
$ vim c.txt #ygzy分支修改c.txt文件

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (ygzy)
$ cat c.txt #查看修改后的内容
ccc
ygzy update

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (ygzy)
$ git add . #添加到暂存
warning: LF will be replaced by CRLF in c.txt.
The file will have its original line endings in your working directory

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (ygzy)
$ git commit -m "ygzy update submit" #提交到版本库
[ygzy 6ba2ea7] ygzy update submit
 1 file changed, 1 insertion(+)

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (ygzy)
$ git checkout master #切换到主分支master
Switched to branch 'master'

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (master)
$ vim c.txt #继续编辑c.txt文件

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (master)
$ cat c.txt #查看c.txt文件内容
ccc
master update

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (master)
$ git add .  #添加到暂存区

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (master)
$ git commit -m "master update submit" #提交到版本库中
[master c012531] master update submit
 1 file changed, 1 insertion(+)

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (master)
$ git merge ygzy #主分支master合并分支ygzy
Auto-merging c.txt
CONFLICT (content): Merge conflict in c.txt #合并c.txt文件时冲突
Automatic merge failed; fix conflicts and then commit the result. 
#自动合并失败,解决冲突然后再提交

$ cat c.txt #查看冲突部分
ccc
<<<<<<< HEAD 
master update #这行是主分支master修改的,需要保留
=======
ygzy update #这样是分支ygzy修改的,需要保留
>>>>>>> ygzy

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (master|MERGING)#出现
冲突时注意这个括号的分支展示
$ vim c.txt #编辑c.txt

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (master|MERGING)
$ cat c.txt #查看编辑后的内容
ccc
master update
ygzy update

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (master|MERGING)
$ git add . #添加到暂存区

lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (master|MERGING)
$ git commit -m "fix conflicts over" #提交
[master 2787944] fix conflicts over
lenovo@DESKTOP-JLQ7CNV MINGW64 ~/Desktop/git_command/pro_git (master)#这里变了(没有了|MERGING)

17:查看用户名和密码
$ git config user.name #查看用户名
ygzy

$ git config user.email #查看邮箱
lz527657138@qq.com

18:设置用户名和密码
$ git config --global user.name "ygzy" #设置用户名
$ git config --global user.email lz527657138@qq.com #设置密码

19:查看其他配置信息(git设置列表)
$ git config --list
core.symlinks=false
core.autocrlf=true
core.fscache=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
help.format=html
......

关于Git的常使用的命令和案例就先到这,后期再完善。

希望可以帮助到你,如若发现问题,请不吝留言,感激不尽!!!!!!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值