Basic operation of gitHub related Git and Github

First, understand Git and Github

1. What is GIT?

Git is a free, open-source version control software

2. What is the version control system?

Version control is a record of one or several file content changes in order to get access to a specific version of the revision of the system in the future.

  • System specific functions

    All change history log files

    At any time to restore any historical status

    Multiplayer collaborative development or modification

    Error Recovery

Version control tools:

  - svn

  - git: software helps users for version management

3. What is Github

Github is the world's largest social programming and code hosting site (https://github.com/).

Github hosts git repository, and provides a web interface (user name .github.io / warehouse name)

4, Github Git and what is the relationship

Git is a version control software

Github project code hosting platform, using git to manage the project code

Second, the use Git

An entrepreneur's story:

Project Background: The entrepreneur wanted to develop a shared system resources

Stage a: smooth on-line codes

First you create a project file in a folder inside the right ---> Click Gir Bash Here ----> git it up and running.

1. Initialization: git init

2, if the first time you use git when you need to configure it. If you have configured, the configuration of the future will not

3, and now the current directory and all sub-files to git: git the Add.     # All files

                            git templates / index.html # add specific files

4, the real data it submitted on-line: git the commit -m "description of "

5, view the status to see whether that has been modified: git status

Must remember to add, after the commit, add later version is not put in, and only commit when only version

6, there are several versions View: View version submit records: git log  

7, roll back to the previous version: git reset --hard version number

We did not commit a generation later when the version number will be randomly generated

8, jump back (see later version number): git reflog 

Git to manage document is divided into two areas of four states.

add to scratch when the state into the green, and when he did not commit in the

This provides rollback functionality.

Stage two: half the time when development occurs bug, bug fixes

  - Development Live functions (development of one week is expected to complete)

When the project development to half of the time, appeared bug how to do? 

Solution: put our code before the code did not become developed, changed completely bug submit. Put away your current development code, find a place to save it.

So how to do it?

Option One: Fix bug: stash

stash files for all workspaces changed obtain a temporary stored in the "somewhere", the workspace to restore the state before the current version is not operated; stash can also be in the "somewhere" in the temporary storage file back again to the work area.

 In particular: the implementation of git stash pop command, you may encounter a conflict, because in an emergency code and bug fixes through stash stored in the "somewhere" code will overlap part, the execution git stash pop time there will be conflict there can be a conflict to resolve the conflict.

Related stash frequently used commands:

  • git stash all the modified contents of the current workspace stored "somewhere", will be restored to the work area has not been modified version of the current state
  • git stash list view "somewhere" all stored records
  • git stash clear empty "somewhere"
  • git stash pop the first record from "somewhere" to get back to work area (potentially conflicting)
  • git stash apply numbers to the specified record number from "somewhere" to get back to work area (potentially conflicting) 
  • git stash drop number specified number of deleted records

When you develop a new code and fix your code manually conflict to resolve the conflict.

方案二:修复bug:利用分支(推荐)

默认会有一个master的分支只做线上的版本,注意不要在master分支上修改,要修改的话就创建一个bug分支

1、创建一个修复bug的分支:git  branch  bug   ,这个bug分支是在master分支上建的,相当于拷贝了一份

2、查看一共有几个分支 :  git  branch

3、跳转到bug分支:git  checkout  bug

4、跳转到master分支:git branch master 

5、创建一个修复bug的分支:git  branch  dev    #只做开发的版本

现在就有三个分支了,每个分支都是不一样的,bug修复完了,现在就要合并在master上了。

6、master和bug分支合并:git mare bug  

7、删除bug分支:git branch -d bug

8、继续开发,切到dev:git  branch  dev

9、开发完毕,master和dev合并:git marge dev 

切换之前一定记得把你的代码提交一下。

合并的时候可能出现冲突,可能不出现,出现冲突了就得手动解决

复制代码
修复Bug流程:
    git branch dev 
    git checkout dev 
                        
    # 写代码
    git add .
    git commit -m 'xxx'
                        
    git checkout master   
    git branch bug 
    git chekcout bug 
                        
    # 修复bug 
    git add .
    git commit -m '紧急修复bug'
                        
    git checkout master 
    git merge bug 
                        
    git branch -d bug 
                        
    git checkout dev 
    # 继续开发
复制代码

阶段三: 两个地方办公

云端需要一个存放代码的地方用Github

  公共的

     - Github是用来做代码托管的

     - 码云,做代码托管

     - csdn code ,做代码托管

  内部搭建:

     - gitlab

1、登录Github

在家里:

创建完成以后

就会有这样一个地址,我们就可以用这个地址来提交

下面是正在往远程推送,完了登录上去就会有mester分支了。

 

当然我们也可以吧dev也推送过来,和上面一样的方式。

在公司:

 克隆

克隆的时候只是把master分支克隆了,其他的怎么拿呢?

用 git pull origin dev   #拿到远程的dev

创建目录

复制代码
                家里:
                    
                    git add .
                    git commit -m 'xx'
                    
                    git remote add origin https://github.com/WuPeiqi/greenlu.git
                    
                    git push origin master 
                    
                公司:
                    
                    # 新公司第一次获取代码
                        方式一
                        git clone https://github.com/WuPeiqi/greenlu.git
                            
                        方式二
                        git init 
                        git remote add origin https://github.com/WuPeiqi/greenlu.git
                        git pull origin master 直接拿回来
                        
                        
                        方式三
                        git init 
                        git remote add origin https://github.com/WuPeiqi/greenlu.git
                        这的两句相当于git pull origin master 这一句
                        git fetch origin master  拿到分支上了
                        git merge origin/master  合并在一起
                    
                    # 日后
                        git pull origin master  #
                        
                        或者
                        git fetch origin master 
                        git merge origin/master 
                        
                        
                        
                    #   如果你在本地修改问题,又add,又提交,在线上去拿的时候,或许会冲突
                    
复制代码

 

 小应用:

vim a1.py #查看内容
cat a1.py #查看内容
当你在家的时候吧代码上线了。到了公司以后吧线上的代码拉回来。git pull origin dev
开始写代码了。
写完之后add commit。。。这时候忘了往github上提交了
回到家的时候,那今天写的代码没提交,怎么办呢?回公司太晚了,在写一遍浪费时间
,或许你还记着今天写的代码,就在这基础上又开发了,
这下提高了警记了,就记得提交了 add 、commit 、git push origin dev
第二天又到公司了,那昨天的代码怎么办呢、、?是拉下来呢?还是推上去呢?
如果你推上去的话,没有冲突还好,如果有冲突的话,那你把你的冲突都上线了,不让你提交,所以我们要先拉下来
先去吧代码拉下来,git pull origin dev 可能会有冲突,解决冲突。

 

一、了解Git和Github

1、什么是GIT?

Git是一个免费、开源的版本控制软件

2、什么是版本控制系统?

版本控制是一种记录一个或若干个文件内容变化,以便将来查阅特定版本修订情况得系统。

  • 系统具体功能

    记录文件的所有历史变化

    随时可恢复到任何一个历史状态

    多人协作开发或修改

    错误恢复

版本控制的工具:

  - svn

  - git  :软件帮助使用者进行版本的管理

3、什么是Github

Github是全球最大的社交编程及代码托管网站(https://github.com/)。

Github可以托管各种git库,并提供一个web界面(用户名.github.io/仓库名)

4、 Github和Git是什么关系

Git是版本控制软件

Github是项目代码托管的平台,借助git来管理项目代码

二、使用Git

一个创业者的故事:

项目背景:这个创业者想开发一个资源共享的系统

阶段一:顺利上线代码

首先在你创建的项目的文件夹里面右键--->点击Gir Bash Here---->吧git运行起来。

1、初始化:git   init

2、如果你第一次使用git的时候,需要配置一下。如果你已经配置了,以后就不用配置了

3、吧当前的目录以及及所有的子文件添加到git:git add .    #所有的文件

                            git templates/index.html   #具体添加的文件

4、真实的吧数据提交上线:git commit -m "描述信息"

5、查看状态,看那个是否被修改过:git status

一定记得先add,后commit,add之后是没有放到版本里的,只有commit的时候才有版本

6、查看有几个版本:查看版本提交记录   :git log  

7、回滚到上一个版本:git   reset --hard   版本号

没一次生成的时候commit后面都会随机的生成版本号

8、再跳回去(查看往后的版本号):git reflog 

Git把管理的文件分为了两个区域四个状态。

add到暂存状态的时候成绿色了,在commit的时候就没有了

提供了这个回滚的功能。

阶段二:当开发到一半的时候出现bug,修复bug

  - 开发直播功能(预计一周开发完成)

当项目开发到一半的时候,出现bug了怎么办? 

解决思路:把我们的代码变成没开发之前的代码,改完bug再提交。先把你当前开发的代码拿走,找个地方先存起来。

那么怎么做呢?

方案一:修复bug:stash

stash用于将工作区发生变化的所有文件获取临时存储在“某个地方”,将工作区还原当前版本未操作前的状态;stash还可以将临时存储在“某个地方”的文件再次拿回到工作区。

 特别的:执行 git stash pop 命令时,可能会遇到冲突,因为在紧急修复bug的代码和通过stash存储在“某个地方”的代码会有重合部分,所以执行 git stash pop 时候就会出现冲突,有冲突解决冲突即可。

stash相关常用命令:

  • git stash             将当前工作区所有修改过的内容存储到“某个地方”,将工作区还原到当前版本未修改过的状态
  • git stash list        查看“某个地方”存储的所有记录
  • git stash clear     清空“某个地方”
  • git stash pop       将第一个记录从“某个地方”重新拿到工作区(可能有冲突)
  • git stash apply     编号, 将指定编号记录从“某个地方”重新拿到工作区(可能有冲突) 
  • git stash drop      编号,删除指定编号的记录

当你新开发的代码和你修复的代码发生冲突的时候就手动解决冲突。

方案二:修复bug:利用分支(推荐)

默认会有一个master的分支只做线上的版本,注意不要在master分支上修改,要修改的话就创建一个bug分支

1、创建一个修复bug的分支:git  branch  bug   ,这个bug分支是在master分支上建的,相当于拷贝了一份

2、查看一共有几个分支 :  git  branch

3、跳转到bug分支:git  checkout  bug

4、跳转到master分支:git branch master 

5、创建一个修复bug的分支:git  branch  dev    #只做开发的版本

现在就有三个分支了,每个分支都是不一样的,bug修复完了,现在就要合并在master上了。

6、master和bug分支合并:git mare bug  

7、删除bug分支:git branch -d bug

8、继续开发,切到dev:git  branch  dev

9、开发完毕,master和dev合并:git marge dev 

切换之前一定记得把你的代码提交一下。

合并的时候可能出现冲突,可能不出现,出现冲突了就得手动解决

复制代码
修复Bug流程:
    git branch dev 
    git checkout dev 
                        
    # 写代码
    git add .
    git commit -m 'xxx'
                        
    git checkout master   
    git branch bug 
    git chekcout bug 
                        
    # 修复bug 
    git add .
    git commit -m '紧急修复bug'
                        
    git checkout master 
    git merge bug 
                        
    git branch -d bug 
                        
    git checkout dev 
    # 继续开发
复制代码

阶段三: 两个地方办公

云端需要一个存放代码的地方用Github

  公共的

     - Github是用来做代码托管的

     - 码云,做代码托管

     - csdn code ,做代码托管

  内部搭建:

     - gitlab

1、登录Github

在家里:

创建完成以后

就会有这样一个地址,我们就可以用这个地址来提交

下面是正在往远程推送,完了登录上去就会有mester分支了。

 

当然我们也可以吧dev也推送过来,和上面一样的方式。

在公司:

 克隆

克隆的时候只是把master分支克隆了,其他的怎么拿呢?

用 git pull origin dev   #拿到远程的dev

创建目录

复制代码
                家里:
                    
                    git add .
                    git commit -m 'xx'
                    
                    git remote add origin https://github.com/WuPeiqi/greenlu.git
                    
                    git push origin master 
                    
                公司:
                    
                    # 新公司第一次获取代码
                        方式一
                        git clone https://github.com/WuPeiqi/greenlu.git
                            
                        方式二
                        git init 
                        git remote add origin https://github.com/WuPeiqi/greenlu.git
                        git pull origin master 直接拿回来
                        
                        
                        方式三
                        git init 
                        git remote add origin https://github.com/WuPeiqi/greenlu.git
                        这的两句相当于git pull origin master 这一句
                        git fetch origin master  拿到分支上了
                        git merge origin/master  合并在一起
                    
                    # 日后
                        git pull origin master  #
                        
                        或者
                        git fetch origin master 
                        git merge origin/master 
                        
                        
                        
                    #   如果你在本地修改问题,又add,又提交,在线上去拿的时候,或许会冲突
                    
复制代码

 

 小应用:

vim a1.py #查看内容
cat a1.py #查看内容
当你在家的时候吧代码上线了。到了公司以后吧线上的代码拉回来。git pull origin dev
开始写代码了。
写完之后add commit。。。这时候忘了往github上提交了
回到家的时候,那今天写的代码没提交,怎么办呢?回公司太晚了,在写一遍浪费时间
,或许你还记着今天写的代码,就在这基础上又开发了,
这下提高了警记了,就记得提交了 add 、commit 、git push origin dev
第二天又到公司了,那昨天的代码怎么办呢、、?是拉下来呢?还是推上去呢?
如果你推上去的话,没有冲突还好,如果有冲突的话,那你把你的冲突都上线了,不让你提交,所以我们要先拉下来
先去吧代码拉下来,git pull origin dev 可能会有冲突,解决冲突。

 

Guess you like

Origin www.cnblogs.com/maaosheng/p/11618746.html