一文搞定GIT版本管理

一文搞下GIT版本管理

有的时候使用git忘记了命令,所以整理一篇方便日后使用,与各位共享。


GIT简介

废话不多说,网上介绍很多,多的不说了,总结一下几点:

  1. GIT和LINUX是同一个大神搞定的,所以开源免费。当然,免费的远程仓库GITHUB里不花钱,托管的代码也是开源的,想保密就得掏钱。
  2. GIT是用C语言开发,内部通过指针控制,所以速度很快。
  3. GIT是分布式分布式版本控制系统;相对于分布式,当然就有集中式。那么区别在哪呢?下面简单介绍:
    • 集中式版本控制系统,版本库是集中存放在中央服务器的,而干活的时候,用的都是自己的电脑,所以要先从中央服务器取得最新的版本,然后开始干活,干完活了,再把自己的活推送给中央服务器。

在这里插入图片描述
- 分布式版本控制系统根本没有“中央服务器”,每个人的电脑上都是一个完整的版本库,这样,你工作的时候,就不需要联网了,因为版本库就在你自己的电脑上。不同人操作后把操作的内容推送给对方就好了。
在这里插入图片描述
**注:图片来及百度搜索


GIT安装

  1. Linux:
sudo apt-get install git
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

按照上面命令操作就行。
2. Mac和Windos:有图像界面直接上官网,有教程,一步一步跟着操作就OK。


GIT常用命令

git init:

初始化,将一个文件夹穿件为GIT版本库,该文件夹中所有内容都会被GIT管理。LINUX和MAC直接通过终端进入指定目录后输入该命令即可,windows在开始菜单打开GIT BASH进入指定目录后输入命令或者在指定文件夹右键,会出现GIT BASH,然后就可以直接在厘米昂输入命令。例如:将~/test_git目录创建为GIT

wtzhu@ubuntu:~/test_git$ git init
Initialized empty Git repository in /home/wtzhu/test_git/.git/

git status:

查看工作区内容的状态,和仓库之间的差异。

git add filename/*:

将工作区的内容提交到暂存区,注意此时还没有提交到仓库,完全提交到仓库需要下一条命令。‘*’表示所有文件

git commit -m ‘注释’:

将暂存区的内容提交到仓库。例:~/test_git目录目前是个空文件夹,我需要在里面创建一个test1.txt文件,并添加hello word进去,并提交到仓库,需要以下操作:

wtzhu@ubuntu:~/test_git$ ls                 # 当前目录下是空的
wtzhu@ubuntu:~/test_git$ git status         # 查看一下状态,因为空的,所以状态也是空的
On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)
wtzhu@ubuntu:~/test_git$ vi test1.txt      # 创建test1.txt,并写入hello world
wtzhu@ubuntu:~/test_git$ cat test1.txt     # 查看test1.txt内容为hello world
hello world
wtzhu@ubuntu:~/test_git$ git add test1.txt # 将test1.txt提交到暂存区
wtzhu@ubuntu:~/test_git$ git status        # 再次查看状态,显示新提交一个test1.txt
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   test1.txt

wtzhu@ubuntu:~/test_git$ git commit -m '第一次提交test1.txt'   # 将test1.txt提交到本地仓库
[master (root-commit) 83d70bb] 第一次提交test1.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test1.txt
wtzhu@ubuntu:~/test_git$ git status    #再次查看状态,提交完了就是空的
On branch master
nothing to commit, working directory clean

git diff filename:

查看文件的不同。例如我修改了test1.txt的内容为hello everyone,再来操作

wtzhu@ubuntu:~/test_git$ cat test1.txt     # 查看内容,被修改为了hello everyone
hello everyone
wtzhu@ubuntu:~/test_git$ git status        # 查看状态
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   test1.txt                   # 显示test1.txt被改动了

no changes added to commit (use "git add" and/or "git commit -a")
wtzhu@ubuntu:~/test_git$ git diff test1.txt    # 查看不同之处
diff --git a/test1.txt b/test1.txt
index 7fd5222..60e2264 100644
--- a/test1.txt
+++ b/test1.txt
@@ -1 +1 @@
-hello word                                 # 原本的内容
+hello everyone                             # 当前文件中的内容
wtzhu@ubuntu:~/test_git$ git add test1.txt 
wtzhu@ubuntu:~/test_git$ git status 
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   test1.txt

wtzhu@ubuntu:~/test_git$ git commit -m '修改内容为hello everyone'
[master e8d0557] 修改内容为hello everyone
 1 file changed, 1 insertion(+), 1 deletion(-)
wtzhu@ubuntu:~/test_git$ git status
On branch master
nothing to commit, working directory clean
wtzhu@ubuntu:~/test_git$ 

git log/git log --pretty=oneline:

查看提交历史。git log查看详细信息,git log --pretty=oneline以一行简要的形式展现。

git reset --hard commitid/HEAD^

版本回退到指定版本,HEAD^表示上一个版本。例如,test1.txt中的内容已经被改为了hello everyone,但是我现在觉得helle world更霸气,需要回去。当然你可直接在文本中操作,但是如果开发,成堆代码不嫌麻烦就没事。GIT提供更方便的形式:

wtzhu@ubuntu:~/test_git$ git log       # 查看提交的日志
commit e8d05571fc8984c36d7b5ccd9b8a7eab89548cdb # commitid
Author: wtzhu <[email protected]>       # usrinfo
Date:   Wed Mar 18 14:37:31 2020 +0800  # date

    修改内容为hello everyone            # 注释

commit 83d70bbc2e00448dd87eb008b246e21d337fed3a
Author: wtzhu <[email protected]>
Date:   Wed Mar 18 14:27:07 2020 +0800

    第一次提交test1.txt
wtzhu@ubuntu:~/test_git$ git log --pretty=oneline  # 一行显示
e8d05571fc8984c36d7b5ccd9b8a7eab89548cdb 修改内容为hello everyone
83d70bbc2e00448dd87eb008b246e21d337fed3a 第一次提交test1.txt
wtzhu@ubuntu:~/test_git$ git reset --hard 83d70bb  # 穿梭回到过去
HEAD is now at 83d70bb 第一次提交test1.txt
wtzhu@ubuntu:~/test_git$ cat test1.txt 
hello word                                          # 穿梭成功
wtzhu@ubuntu:~/test_git$ 

git checkout – filename:

把工作区的内容回撤到更改前,此时未被提交的暂存区。例如,在test1.txt中不小心加入了err,想要删掉,可以手动删掉,但是如果修改了代码里的部分参数,发现之前的参数更好,一个个换回之前的参数太费劲或者忘了改了哪些,此时就用改命令

wtzhu@ubuntu:~/test_git$ cat test1.txt 
hello word
err                                                 # 不小心修改的内容
wtzhu@ubuntu:~/test_git$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   test1.txt

no changes added to commit (use "git add" and/or "git commit -a")
wtzhu@ubuntu:~/test_git$ git checkout -- test1.txt # 撤销修改
wtzhu@ubuntu:~/test_git$ cat test1.txt             # 修改的内容已经改回来了
hello word

git reset HEAD :

与上一条的作用相同,不同之处在于这个是撤销已经提交到暂存区的内容,将暂存区的内容返还到工作区,如果需要修改会最初状态,接接着使用上一条命令

hello word
err
wtzhu@ubuntu:~/test_git$ git add test1.txt 
wtzhu@ubuntu:~/test_git$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   test1.txt

wtzhu@ubuntu:~/test_git$ git reset HEAD test1.txt      # 将缓冲区的内容撤回到工作区
Unstaged changes after reset:
M	test1.txt
wtzhu@ubuntu:~/test_git$ cat test1.txt                 # 此时工作区内容还是错误的
hello word
err
wtzhu@ubuntu:~/test_git$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   test1.txt

no changes added to commit (use "git add" and/or "git commit -a")
wtzhu@ubuntu:~/test_git$ git checkout -- test1.txt
wtzhu@ubuntu:~/test_git$ cat test1.txt 
hello word                                              # 完全恢复正常

git rm:

删除仓库某个文件。例如在当前目录下创建test2.txt并提交到仓库,然后在工作区又删掉了test2.txt,需要把仓库也改回来,就使用这个,当然这个和git add一样,操作完后需要git commit一下

wtzhu@ubuntu:~/test_git$ touch test2.txt
wtzhu@ubuntu:~/test_git$ ls
test1.txt  test2.txt
wtzhu@ubuntu:~/test_git$ git add *
wtzhu@ubuntu:~/test_git$ git commit -m 'add test2.txt'
[master 9f7f5fc] add test2.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test2.txt
wtzhu@ubuntu:~/test_git$ git status
On branch master
nothing to commit, working directory clean
wtzhu@ubuntu:~/test_git$ rm test2.txt 
wtzhu@ubuntu:~/test_git$ ls
test1.txt
wtzhu@ubuntu:~/test_git$ git status            # 查看状态显示删除了一个文件
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	deleted:    test2.txt

no changes added to commit (use "git add" and/or "git commit -a")
wtzhu@ubuntu:~/test_git$ git rm test2.txt      # 删除一个文件
rm 'test2.txt'
wtzhu@ubuntu:~/test_git$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	deleted:    test2.txt

wtzhu@ubuntu:~/test_git$ git commit -m 'delete test2.txt'  # 提交到仓库
[master e9789a4] delete test2.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 test2.txt
wtzhu@ubuntu:~/test_git$ git status            # 恢复原状
On branch master
nothing to commit, working directory clean

git branch [name]:

git branch [name]创建一个名为name的分支,git branch查看现有分支

wtzhu@ubuntu:~/test_git$ git branch fred
wtzhu@ubuntu:~/test_git$ git branch
  fred
* master

创建了一个名为fred的分支,*所指是当前分支

git checkout :

切换到某个分支,例如从master分支切换到fred分支

wtzhu@ubuntu:~/test_git$ git checkout fred
Switched to branch 'fred'
wtzhu@ubuntu:~/test_git$ git branch
* fred
  master

git merge :

在当前分支合并某一分支,例如我再fred分支上修改了test1.TXT文件,并提交到仓库,回到master分支还没有改动,此时,把fred分支合并过来,master分支就会改过来。

wtzhu@ubuntu:~/test_git$ cat test1.txt 
hello word
add fred branch
wtzhu@ubuntu:~/test_git$ git add test1.txt 
wtzhu@ubuntu:~/test_git$ git commit -m 'add fred branch in test1.txt'
[fred fa7b7bc] add fred branch in test1.txt
 1 file changed, 1 insertion(+)
 wtzhu@ubuntu:~/test_git$ git log --pretty=oneline
fa7b7bc19a08c55b8550560a146620f0f5f84a18 add fred branch in test1.txt   # 此时在fred分支上已经别提交到仓库
16d286199cc107e2a558f25febc974e611bf09f4 del 1.txt
b25768d5e9ee468d899bb6a8ef0d0713c2b51508 add 1.txt
e9789a48cd35b8bc8b5346f71725666f603a2265 delete test2.txt
9f7f5fc2dcd47392f49bb013a1bdba07efac14bd add test2.txt
83d70bbc2e00448dd87eb008b246e21d337fed3a 第一次提交test1.txt
wtzhu@ubuntu:~/test_git$ git checkout master 
Switched to branch 'master'
wtzhu@ubuntu:~/test_git$ git log --pretty=oneline                      # 回到master分支发现还没有提交
16d286199cc107e2a558f25febc974e611bf09f4 del 1.txt
b25768d5e9ee468d899bb6a8ef0d0713c2b51508 add 1.txt
e9789a48cd35b8bc8b5346f71725666f603a2265 delete test2.txt
9f7f5fc2dcd47392f49bb013a1bdba07efac14bd add test2.txt
83d70bbc2e00448dd87eb008b246e21d337fed3a 第一次提交test1.txt
wtzhu@ubuntu:~/test_git$ cat test1.txt     # 内容确实没有被修改
hello word
wtzhu@ubuntu:~/test_git$ git merge fred    # 合并完成后就恢复过来了
Updating 16d2861..fa7b7bc
Fast-forward
 test1.txt | 1 +
 1 file changed, 1 insertion(+)
wtzhu@ubuntu:~/test_git$ git log --pretty=oneline
fa7b7bc19a08c55b8550560a146620f0f5f84a18 add fred branch in test1.txt
16d286199cc107e2a558f25febc974e611bf09f4 del 1.txt
b25768d5e9ee468d899bb6a8ef0d0713c2b51508 add 1.txt
e9789a48cd35b8bc8b5346f71725666f603a2265 delete test2.txt
9f7f5fc2dcd47392f49bb013a1bdba07efac14bd add test2.txt
83d70bbc2e00448dd87eb008b246e21d337fed3a 第一次提交test1.txt
wtzhu@ubuntu:~/test_git$ cat test1.txt 
hello word
add fred branch

git branch -d :

删除分支,当一个分支用完了,不需要了就删掉,例如fred分支的作用就是加一句,使命完成后不需要了就删掉

wtzhu@ubuntu:~/test_git$ git branch -d fred    # 删除fred分支
Deleted branch fred (was fa7b7bc).
wtzhu@ubuntu:~/test_git$ git branch            # 再次查看就没有了
* master

当然在合并分支的时候也会出现意外冲突,例如,我再创建一个Tom分支,并添加一句add tom branch,在master分支我也添加了内容look master,此时合并就会出现问题
wtzhu@ubuntu:~/test_git$ git branch tom
wtzhu@ubuntu:~/test_git$ git checkout tom
Switched to branch 'tom'
wtzhu@ubuntu:~/test_git$ vi test1.txt 
wtzhu@ubuntu:~/test_git$ cat test1.txt 
hello word
add fred branch
add tom branch
wtzhu@ubuntu:~/test_git$ git add *
wtzhu@ubuntu:~/test_git$ git commit -m 'add tom branch in test1.txt'
[tom d80f9ce] add tom branch in test1.txt
 1 file changed, 1 insertion(+)
wtzhu@ubuntu:~/test_git$ git log --pretty=oneline
d80f9cec849aec87e924e2e1d6524461ae98947d add tom branch in test1.txt  # Tom分支已经正常提交
fa7b7bc19a08c55b8550560a146620f0f5f84a18 add fred branch in test1.txt
16d286199cc107e2a558f25febc974e611bf09f4 del 1.txt
b25768d5e9ee468d899bb6a8ef0d0713c2b51508 add 1.txt
e9789a48cd35b8bc8b5346f71725666f603a2265 delete test2.txt
9f7f5fc2dcd47392f49bb013a1bdba07efac14bd add test2.txt
83d70bbc2e00448dd87eb008b246e21d337fed3a 第一次提交test1.txt
wtzhu@ubuntu:~/test_git$ git checkout master 
Switched to branch 'master'
wtzhu@ubuntu:~/test_git$ ls
test1.txt
wtzhu@ubuntu:~/test_git$ cat test1.txt 
hello word
add fred branch
wtzhu@ubuntu:~/test_git$ vi test1.txt 
wtzhu@ubuntu:~/test_git$ git add *
wtzhu@ubuntu:~/test_git$ git commit -m 'add look master in test1.txt'
[master ccd8b57] add look master in test1.txt
 1 file changed, 1 insertion(+)
wtzhu@ubuntu:~/test_git$ git log --pretty=oneline 
ccd8b5747b637719bf0c16fce5721c48ed5ec780 add look master in test1.txt   # master分支正常提交
fa7b7bc19a08c55b8550560a146620f0f5f84a18 add fred branch in test1.txt
16d286199cc107e2a558f25febc974e611bf09f4 del 1.txt
b25768d5e9ee468d899bb6a8ef0d0713c2b51508 add 1.txt
e9789a48cd35b8bc8b5346f71725666f603a2265 delete test2.txt
9f7f5fc2dcd47392f49bb013a1bdba07efac14bd add test2.txt
83d70bbc2e00448dd87eb008b246e21d337fed3a 第一次提交test1.txt
wtzhu@ubuntu:~/test_git$ cat test1.txt 
hello word
add fred branch
look master
wtzhu@ubuntu:~/test_git$ git merge tom     # 合并分支报错,因为两个版本都做了修改,GIT不知道哪个才是想要的,需要手动选择一下
Auto-merging test1.txt
CONFLICT (content): Merge conflict in test1.txt
Automatic merge failed; fix conflicts and then commit the result.
wtzhu@ubuntu:~/test_git$ cat test1.txt 
hello word
add fred branch
<<<<<<< HEAD
look master
=======
add tom branch
>>>>>>> tom
wtzhu@ubuntu:~/test_git$ vi test1.txt      # 手动编辑一下test1.txt
wtzhu@ubuntu:~/test_git$ git add test1.txt 
wtzhu@ubuntu:~/test_git$ git commit -m 'merge tom to master'   # 编辑完成后再次提交一下,此时就讲tom分支内容合并过来了
[master 56d07f4] merge tom to master
wtzhu@ubuntu:~/test_git$ cat test1.txt 
hello word
add fred branch
look master
add tom branch

wtzhu@ubuntu:~/test_git$ git log --pretty=oneline 
56d07f423affcbb9018762288e23ff23c0daef17 merge tom to master
ccd8b5747b637719bf0c16fce5721c48ed5ec780 add look master in test1.txt
d80f9cec849aec87e924e2e1d6524461ae98947d add tom branch in test1.txt
fa7b7bc19a08c55b8550560a146620f0f5f84a18 add fred branch in test1.txt
16d286199cc107e2a558f25febc974e611bf09f4 del 1.txt
b25768d5e9ee468d899bb6a8ef0d0713c2b51508 add 1.txt
e9789a48cd35b8bc8b5346f71725666f603a2265 delete test2.txt
9f7f5fc2dcd47392f49bb013a1bdba07efac14bd add test2.txt
83d70bbc2e00448dd87eb008b246e21d337fed3a 第一次提交test1.txt

git log --pretty=oneline --grep:

查看提交记录,用图的形式展示分支合并

wtzhu@ubuntu:~/test_git$ git log --pretty=oneline --graph
*   56d07f423affcbb9018762288e23ff23c0daef17 merge tom to master
|\  
| * d80f9cec849aec87e924e2e1d6524461ae98947d add tom branch in test1.txt
* | ccd8b5747b637719bf0c16fce5721c48ed5ec780 add look master in test1.txt
|/  
* fa7b7bc19a08c55b8550560a146620f0f5f84a18 add fred branch in test1.txt
* 16d286199cc107e2a558f25febc974e611bf09f4 del 1.txt
* b25768d5e9ee468d899bb6a8ef0d0713c2b51508 add 1.txt
* e9789a48cd35b8bc8b5346f71725666f603a2265 delete test2.txt
* 9f7f5fc2dcd47392f49bb013a1bdba07efac14bd add test2.txt
* 83d70bbc2e00448dd87eb008b246e21d337fed3a 第一次提交test1.txt

远程仓库相关的命令,最直接的远程仓库就是github,可以拿这几个来试试

  • git remote add origin git@server-name:path/repo-name.git关联一个远程库
  • git push -u origin master第一次推送master分支的所有内容
  • git push origin master推送最新修改

友情提示:

记住以上命令基本上可以搞定git,平时使用时多看一下git的提示信息,里面会给出很多重要提示。

发布了14 篇原创文章 · 获赞 0 · 访问量 2393

猜你喜欢

转载自blog.csdn.net/wtzhu_13/article/details/104946976