GIT版本管理

git管理项目


一、Git基础

配置

使用Git前需要做的最小配置:

git config --global user.name 'dagger'
git config --global user.email 'dagger.163.com'

config三个作用域:local只对某个仓库有效,global当前用户的所有仓库有效,system系统登陆所有用户有效;

git config --local  
git config --global
git config --system 

显示config配置,加–list;不加local等作用域显示全部的配置;

git config --list --local 
git config --list --global
git config --list --system 

创建Git仓库

场景一:已有项目代码;

cd project
git init

场景二:全新的项目;

git init project
cd project

命令行查看版本信息

常规版本信息 --oneline 、-n 、–all、graph 可以叠加使用

git log 

查看指定分支版本信息

git log master

简单版本信息

git log --oneline

有筛选条数的版本信息,例前10条版本信息

git log -n10

有筛选条数的简单的版本信息,例前10条版本信息

git log -n10 --oneline

查看当前所有分支指向的提交的commit

git log -n10 --all

查看当前所有分支commit图形化的演变情况

git log -n10 --all --graph

图形界面Gitk查看版本信息

在这里插入图片描述

远程仓库管理

检出仓库

$ git clone git://github.com/jquery/jquery.git

查看远程仓库

$ git remote -v

添加远程仓库

$ git remote add [name] [url]

删除远程仓库

$ git remote rm [name]

修改远程仓库

$ git remote set-url --push[name][newUrl]

拉取远程仓库

$ git pull [remoteName] [localBranchName]

推送远程仓库

$ git push [remoteName] [localBranchName]

文件管理

重命名文件

git mv old_file new_file

删除文件

git  rm file_name #删除file_name 文件

分支管理

查看本地分支

$ git branch

查看远程分支

$ git branch -r

创建本地分支

$ git branch [name] ----注意新分支创建后不会自动切换为当前分支

创建本地分支(远程分支为模板)

$ git checkout -b xxxx(本地分支名称) yyyy(上条命令查找到的远程分支的名称)

切换分支

$ git checkout [name]

创建新分支并立即切换到新分支

$ git checkout -b [name]

删除分支
*-d选项只能删除已经参与了合并的分支,对于未有合并的分支是无法删除的。如果想强制删除一个分支,可以使用-D选项

 git branch -d [name]  

合并分支

$ git merge [name] # 将名称为[name]的分支与当前分支合并

创建远程分支(本地分支push到远程)

$ git push origin [name]

删除远程分支

$ git push origin :heads/[name]

比较(diff)

1.怎么比较工作区和暂存区所含文件的差异?

git diff
git diff index.php #单独比较index.php

2.怎么比较暂存区和HEAD所含文件的差异?

git diff --cached 
git diff --cached index.php #单独比较index.php

3.查看不同分支文件的差异

git diff test master 比较test与master分支
git diff test master -- index.php #比较test与master分支的index.php文件

文件恢复(reset /checkout)

  1. 如何让暂存区恢复成和HEAD的一样?
git reset HEAD 
git reset index.php #单独恢复暂存文件 index.php
  1. 如何让工作区的文件恢复为和暂存区一样?
git checkout index.php #单独恢复暂存文件 index.php

二、独自使用Git常用场景

修改commit的message

1.修改commit的当前的message
git commit --amend  #进入vim模式I编辑模式时候编辑想要编辑的信息:wq!保存即可
test 分支第一次提交 #当前commit的message ,直接修改保存该行就修改好了
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
## 此处省略....
2.修改commit旧的某一个message

当前的git log

$ git log
commit 7c535deab02d1d9753b38bf962fc1009c9f01ee1 (HEAD -> test)
Author: dagger <731619080@qq.com>
Date:   Thu Jul 7 23:50:40 2022 +0800

   test第三次提交

commit a47cbe9ef03e8afd63a13ed3a4406c48e144d2dc
Author: dagger <731619080@qq.com>
Date:   Thu Jul 7 23:49:14 2022 +0800

   test第二次提交

commit 807231fa863933b5bebb0841db1f03ffe44287f3
Author: dagger <731619080@qq.com>
Date:   Thu Jul 7 17:32:01 2022 +0800

   重新修改 messahe 'test 分支第一次提交'

commit 05ad398a674286d570dc672bea57b5f385afae4c (master)
Author: dagger <731619080@qq.com>
Date:   Thu Jul 7 17:22:45 2022 +0800

   第一次提交

Dagger@dagger MINGW64 /d/phpstudy_pro/WWW/test-git (test)
$

把message“ 重新修改 messahe ‘test 分支第一次提交’”修改“ test 分支第一次提交”

git commit --rebase -i  05ad398a67428

交互界面1
pick 807231f 重新修改 messahe ‘test 分支第一次提交’ 这一行的pick改成reword

pick 807231f 重新修改 messahe 'test 分支第一次提交'
pick a47cbe9 test第二次提交
pick 7c535de test第三次提交

## 此处省略....

交互界面2
直接修改message "重新修改 messahe 'test 分支第一次提交"改成"test 第一次提交"保存退出

重新修改 messahe 'test 分支第一次提交'

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
## 此处省略....

修改好的git log

$ git log
commit 8992937d838af47da542f9b34e5b1c4fbeaad4f6 (HEAD -> test)
Author: dagger <731619080@qq.com>
Date:   Thu Jul 7 23:50:40 2022 +0800

    test第三次提交

commit 0d68b35bf669ff807d54b98654d52cc44cd3cd92
Author: dagger <731619080@qq.com>
Date:   Thu Jul 7 23:49:14 2022 +0800

    test第二次提交

commit 362b5326e1d4f2fd94e39338471c99eca2d54d6a
Author: dagger <731619080@qq.com>
Date:   Thu Jul 7 17:32:01 2022 +0800

    test第一次提交

commit 05ad398a674286d570dc672bea57b5f385afae4c (master)
Author: dagger <731619080@qq.com>
Date:   Thu Jul 7 17:22:45 2022 +0800

    第一次提交

Dagger@dagger MINGW64 /d/phpstudy_pro/WWW/test-git (test)
$
2 怎样把连续的多个commit整理成1个

当前的git log

$ git log --graph
* commit a2f0bc835d99cb7c9f073e8f1c27ff47cb55374c (HEAD -> test)
| Author: dagger <731619080@qq.com>
| Date:   Fri Jul 8 10:47:46 2022 +0800
|
|     test增加index.php
|
* commit 8992937d838af47da542f9b34e5b1c4fbeaad4f6
| Author: dagger <731619080@qq.com>
| Date:   Thu Jul 7 23:50:40 2022 +0800
|
|     test第三次提交
|
* commit 0d68b35bf669ff807d54b98654d52cc44cd3cd92
| Author: dagger <731619080@qq.com>
| Date:   Thu Jul 7 23:49:14 2022 +0800
|
|     test第二次提交
|
* commit 362b5326e1d4f2fd94e39338471c99eca2d54d6a
| Author: dagger <731619080@qq.com>
| Date:   Thu Jul 7 17:32:01 2022 +0800
|
|     test第一次提交
|
* commit 05ad398a674286d570dc672bea57b5f385afae4c (master)
  Author: dagger <731619080@qq.com>
  Date:   Thu Jul 7 17:22:45 2022 +0800

      第一次提交

开始合并

git rebase -i 05ad398a67

交互界面1;test第二次提交,test第三次提交 两处的pick 改为squash。test第一次/第二次/第三次这三次提交合并为一个

pick 362b532 test第一次提交
pick 0d68b35 test第二次提交
pick 8992937 test第三次提交
pick a2f0bc8 test增加index.php
## 此处省略....

交互界面2 在 # This is a combination of 3 commits与#This is the 1st commit message写合并多个提交操作的提示信息

# This is a combination of 3 commits.
test分支增加index.htnl 和 reset.bat 文件
# This is the 1st commit message:

test第一次提交

# This is the commit message #2:

test第二次提交

# This is the commit message #3:

test第三次提交

## 此处省略....

合并好的git log

$ git log --graph
* commit 3573534a19f40a02d9b3b399552b9452de1139fd (HEAD -> test)
| Author: dagger <731619080@qq.com>
| Date:   Fri Jul 8 10:47:46 2022 +0800
|
|     test增加index.php
|
* commit 4a94d0d585d8aec5a54f7bf1cfcb25d70cb92c14
| Author: dagger <731619080@qq.com>
| Date:   Thu Jul 7 17:32:01 2022 +0800
|
|     test分支增加index.htnl和rebase.bat文件
|
|     test第一次提交
|
|     test第二次提交
|
|     test第三次提交
|
* commit 05ad398a674286d570dc672bea57b5f385afae4c (master)
  Author: dagger <731619080@qq.com>
  Date:   Thu Jul 7 17:22:45 2022 +0800

      第一次提交
3. 怎样把间隔的几个commit整理成1个?

交互界面1

pick 4a94d0d test分支增加index.htnl和rebase.bat文件
pick 3573534 test增加index.php
pick 821f2d5 test第四次提交
## 此处省略....

改为

pick 4a94d0d test分支增加index.htnl和rebase.bat文件
squash 821f2d5 test第四次提交
pick 3573534 test增加index.php

交互界面2

# This is a combination of 2 commits.
test分支增加index.htnl和rebase.bat文件初始化 #新的合并操作的message信息
# This is the 1st commit message:

test分支增加index.htnl和rebase.bat文件 

test第一次提交

test第二次提交

test第三次提交

# This is the commit message #2:

test第四次提交

合并好的git log

$ git log --graph
* commit f894f80e740f05dafcc437b692b26833cf94004d (HEAD -> test)
| Author: dagger <731619080@qq.com>
| Date:   Fri Jul 8 10:47:46 2022 +0800
|
|     test增加index.php
|
* commit 0d1303f33f13b35552eda1ed3b411a33539bbcf2
| Author: dagger <731619080@qq.com>
| Date:   Thu Jul 7 17:32:01 2022 +0800
|
|     test分支增加index.htnl和rebase.bat文件初始化
|
|     test分支增加index.htnl和rebase.bat文件
|
|     test第一次提交
|
|     test第二次提交
|
|     test第三次提交
|
|     test第四次提交
|
* commit 05ad398a674286d570dc672bea57b5f385afae4c (master)
  Author: dagger <731619080@qq.com>
  Date:   Thu Jul 7 17:22:45 2022 +0800

      第一次提交

代码回滚(reset --hard)

1. 消除最近的几次提交

查看当前log

$ git log --oneline
94e67c9 (HEAD -> test) git diff --cached
f894f80 test增加index.php
0d1303f test分支增加index.htnl和rebase.bat文件初始化
05ad398 (master) 第一次提交

回滚到“test分支增加index.htnl和rebase.bat文件初始化”

git reset --hard 0d1303f 

查看当前log

$ git log --oneline
0d1303f (HEAD -> test) test分支增加index.htnl和rebase.bat文件初始化
05ad398 (master) 第一次提交

二、如何指定不需要Git管理的文件?

三、开发中临时加塞了紧急任务怎么处理?

1.添加 git stash

$ git stash 添加stash版本
Saved working directory and index state WIP on test: 94e67c9 git diff --cached

git stash save 添加有备注的stash版本

git stash save "优惠券分组" 

2.stash list 查看stash版本信息

$ git stash list
stash@{0}: On test: git stash apply
stash@{1}: On test: 优惠券分组
stash@{2}: WIP on test: 94e67c9 git diff --cached

3.使用版本
A: stash apply 不删除stash存储的版本 工作区恢复到指定版本

git stash apply #未指定版本默认获取最近一次stash 0 ----> git stash apply
git stash apply 1 #指定版本1 -----> stash@{1}: On test: 优惠券分组

B: stash pop 删除stash存储的版本 工作区恢复到指定版本

git stash pop #未指定版本默认获取最近一次stash 0 ----> git stash apply
git stash pop  1 #指定版本1 -----> stash@{1}: On test: 优惠券分组
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值