码迷,mamicode.com
首页 > 其他好文 > 详细

GIT&GITHUB

时间:2018-09-11 14:02:45      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:多个   conf   str   标示   name   git   结构   not   执行   

Git&GitHub

版本控制

个人版本控制

在进行开发的时候,程序需要经常性的修改,那么修改的时候就存在备份问题,当代码量较大修改频繁时,备份的及时性和备份的困难度就增加了。

团队版本控制

当团队中多人协同开发同一个功能时,可能存在开发人员之间的版本互相覆盖问题。

版本控制特点

  1. 协同修改

    对人可以并行地修改同一个文件。

  2. 数据备份

    不仅保存数据当前地实际状态,还能保存文件提交地历史状态,可以回滚操作。

  3. 版本管理

    保存版本时不保存重复数据,节约存储,提高运行效率。

  4. 权限控制

    对团队中参与开发的人员进行权限控制,对其他开发人员贡献的代码进行审核。

  5. 历史记录

    查看提交修改历史,版本回滚

  6. 分支管理

    允许开发团队在工作过程中多条生产线同时推进任务,提高效率。

    工具简介

集中式:SVN、CVS、VSS。以服务器为核心,容易出现单点故障问题。

技术分享图片

分布式:GIT、Mercurial、Bazaar、Darcs,避免了单点故障。

技术分享图片

GIT

技术分享图片

优势

  1. 版本控制可在本地完成
  2. 完整性保证
  3. 添加大于删除和修改
  4. 分支操作快捷流畅
  5. bash融合

结构

技术分享图片

技术分享图片

  • Directory:使用Git管理的一个目录,也就是一个仓库,包含我们的工作空间和Git的管理空间。
  • WorkSpace:需要通过Git进行版本控制的目录和文件,这些目录和文件组成了工作空间,除了.git之外的都属于工作区。
  • .git:存放Git管理信息的目录,初始化仓库的时候自动创建。
  • Index/Stage:暂存区,或者叫待提交更新区,在提交进入repo之前,我们可以把所有的更新放在暂存区。
  • Local Repo:本地仓库,一个存放在本地的版本库;HEAD会只是当前的开发分支(branch)。
  • Stash:是一个工作状态保存栈,用于保存/恢复WorkSpace中的临时状态。

本地操作

初始化本地库

git init                    //在目录中将创建一个.git目录

技术分享图片

设置签名

设置一个用户信息,用于唯一标示身份,仅用于区分开发人员,信息和远程仓库无关。

签名分为项目级别和系统级别,签名的作用域不同,项目/仓库级别在本仓库中有效,优先使用仓库级别。签名必须设置才能执行操作。

git config user.name xxx
git config user.email xxx@xxx.com

git config --global user.name xxx
git config user.email xxx@xxx.com
$ cat ~/.gitconfig
[user]
        email = 1958862281@qq.com
        name = Wan9huan
        
//全局签名存储在全局配置文件中
//仓库签名保存在.git/config中

常用命令

git status                                              查看状态
git add                                                     添加文件到缓存区
git remove --cached                             移除已经add的文件
git commit                                              提交
git commit -a -m"This is Message"   提交附带注释
git log                                                     查看日志
git log --oneline                                   简洁查看
git relog                                                   查看日志和回滚步数
git reset --hard HEAD^                      后退一步
git reset --hard HEAD~3                     后退三步
git reset --hard 2fedc85                    回到指定版本
--------------------------------------------------------------------------------
$ git status

On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

--------------------------------------------------------------------------------
$ git status

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

        ReactDemo/
        2017-09-07.png

nothing added to commit but untracked files present (use "git add" to track)
--------------------------------------------------------------------------------
$ git add 2017-09-07.png
$ git status

On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)      // 从缓存区移除
            
        new file:   2017-09-07.png

Untracked files:
  (use "git add <file>..." to include in what will be committed)
--------------------------------------------------------------------------------
$ git commit

[master (root-commit) c40dd30]  Changes to be committed: new file:   2017-09-07.png
                                            //版本号
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 2017-09-07.png
 --------------------------------------------------------------------------------
 $ git status
 
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
--------------------------------------------------------------------------------
$ git log --oneline

8b6ba42 (HEAD -> master) Create A Book Text
37c34e8         deleted:    Photoshop_Set-Up.exe
4cfa483 (origin/master) last
a2858e8 new
4b3e03e aa
dc03544 Merge branch 'master' of https://github.com/wan9huan/myfiles
9b3d010 all
9382437 Delete background.png
bb31bd9         Changes to be committed:        new file:   icon 2.png
b386c61 a
4366632 a
fc86b6a png for background
--------------------------------------------------------------------------------
$ git reflog

8b6ba42 (HEAD -> master) HEAD@{0}: commit: Create A Book Text
37c34e8 HEAD@{1}: commit: deleted: Photoshop_Set-Up.exe
4cfa483 (origin/master) HEAD@{2}: commit: last
a2858e8 HEAD@{3}: commit: new
4b3e03e HEAD@{4}: commit: aa
dc03544 HEAD@{5}: pull: Merge made by the 'recursive' strategy.
9b3d010 HEAD@{6}: commit: all
bb31bd9 HEAD@{7}: commit: Changes to be committed:
b386c61 HEAD@{8}: commit: a
4366632 HEAD@{9}: commit: a
fc86b6a HEAD@{10}: commit (initial): png for background
--------------------------------------------------------------------------------
$ git reflog book.txt
5997266 (HEAD -> master) HEAD@{0}: commit: 第四次修改
2fedc85 HEAD@{1}: commit: 第三次修改
af72196 HEAD@{2}: commit: add a string
8b6ba42 HEAD@{3}: commit: Create A Book Text
--------------------------------------------------------------------------------
//回滚、回到指定版本
$ git reflog book.txt
5997266 (HEAD -> master) HEAD@{0}: commit: 第四次修改
2fedc85 HEAD@{1}: commit: 第三次修改
af72196 HEAD@{2}: commit: add a string
8b6ba42 HEAD@{3}: commit: Create A Book Text
$ cat book.txt
This is a Book Text
333333333333333
444444444444444

$ git reset --hard af72196
HEAD is now at af72196 add a string
$ cat book.txt
This is a Book Text

$ git reset --hard 2fedc85
HEAD is now at 2fedc85 第三次修改
$ cat book.txt
This is a Book Text
333333333333333
--------------------------------------------------------------------------------
$ git reset --hard HEAD^    后退一步
$ git reset --hard HEAD~3 后退三步

Rest指令

git rest --soft             移动本地库指针
git rest --mixed            移动本地库、重置暂存区
git rest --hard             移动本地库、重置暂存区、重置工作区

比较文件差异

git diff                            暂存区比较
git diff Head                   比较某一版本
--------------------------------------------------------------------------------
$ git diff book.txt

diff --git a/book.txt b/book.txt
index 48d8508..f2259a8 100644
--- a/book.txt
+++ b/book.txt
@@ -1,2 +1,4 @@
 This is a Book Text
-333333333333333
\ No newline at end of file
+333333333333333
+444444444444444
+555555555555555
\ No newline at end of file

分支

技术分享图片

  1. 并行推进多个开发
  2. 分支彼此独立
  3. 热修复
git status                                      查看当前分支
git branch -v                                   查看所有分支
git branch xxx                              创建分支
git checkout xxx                            切换分支
--------------------------------------------------------------------------------
$ git status -v

On branch master                //主分支
Your branch is ahead of 'origin/master' by 5 commits.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean

$ git branch -v
* master 828b1ab [ahead 5] commit all

$ git branch hot_fix

$ git checkout hot_fix
Switched to branch 'hot_fix'
--------------------------------------------------------------------------------
#分支合并
1.切换到主分支
2.执行 git merge xxxx
--------------------------------------------------------------------------------
git checkout xxxx
git merge xxxx
--------------------------------------------------------------------------------

冲突解决

  1. 发生文件冲突,进入冲突解决状态
  2. 打开文件可以查看到冲突内容
  3. 删除冲突标记并修改文件
  4. 提交完成合并分支

GIT&GITHUB

标签:多个   conf   str   标示   name   git   结构   not   执行   

原文地址:https://www.cnblogs.com/Wu-Zang/p/9626806.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!