git实验

 

四、实例应用

应用1、现有项目移植到git代管

  进入目标项目,进行git初始化:

  初始化:git init

  修改config:git config -- local user.name '名称'  和  git config -- local user.email '邮箱'

# 初始化
git init

# 此时生成.git/文件夹
$ ls -al | grep git
drwxr-xr-x 1 fat39 197121   0 3月   9 14:23 .git/
初始化
# 先设置。如无,在commit时会提示需要name和email。
git config -- local user.name 'fat39'
git config -- local user.email 'fat39@163.com'
修改config

  查看文件状态,进行如下操作:

  修改过的内容 --> 暂存区   git add .

  暂存区 --> 分支  git commit -m "初次移植到git"

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git status
On branch master

No commits yet

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

        .idea/
        FOR_BLOG/
        。。。。略。。。。

nothing added to commit but untracked files present (use "git add" to track)

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git add .

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git status
On branch master

No commits yet

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

        new file:   .idea/FOR_BLOG.iml
        。。。。略。。。。


fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git commit -m "初次移植到git"
[master (root-commit) d918781] 初次移植到git
 31 files changed, 11386 insertions(+)
 create mode 100644 .idea/FOR_BLOG.iml
 。。。。略。。。。

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git log
commit d91878163e1d748a9c7d1921742fd69151a53f98 (HEAD -> master)
Author: fat39 <fat39@163.com>
Date:   Fri Mar 9 14:27:29 2018 +0800

    初次移植到git
移植

  查看目录、文档

  git ls-tree head #查看版本中所有文件
  git ls-files -s #查看暂存区和版本中所有文件

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git ls-tree head
040000 tree 316d37b9d5d035baeed1117f2551423791d9f4e1    .idea
040000 tree 61f74461abfc45a63bf7e22b821a5d1df19f87c2    FOR_BLOG
040000 tree f0f9c73d8df916b5d3c53f33ea80d313ba410599    app01
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391    db.sqlite3
100644 blob cc943662e5e3a1b60424d76c6693804dc26d470a    manage.py
040000 tree 791808501055d618dc4c212d01ef02297c9ede09    static
040000 tree c80f154fdfa5ff8038566cff95922c1f17153df8    templates

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git ls-files
.idea/FOR_BLOG.iml
.idea/encodings.xml
.idea/misc.xml
.idea/modules.xml
.idea/vcs.xml
.idea/workspace.xml
FOR_BLOG/__init__.py
FOR_BLOG/__pycache__/__init__.cpython-36.pyc
FOR_BLOG/__pycache__/settings.cpython-36.pyc
FOR_BLOG/__pycache__/urls.cpython-36.pyc
FOR_BLOG/__pycache__/wsgi.cpython-36.pyc
FOR_BLOG/settings.py
FOR_BLOG/urls.py
FOR_BLOG/wsgi.py
app01/__init__.py
app01/__pycache__/__init__.cpython-36.pyc
app01/__pycache__/admin.cpython-36.pyc
app01/__pycache__/apps.cpython-36.pyc
app01/__pycache__/models.cpython-36.pyc
app01/__pycache__/views.cpython-36.pyc
app01/admin.py
app01/apps.py
app01/migrations/__init__.py
app01/migrations/__pycache__/__init__.cpython-36.pyc
app01/models.py
app01/tests.py
app01/views.py
db.sqlite3
manage.py
static/jquery-3.3.1.js
templates/index.html
查看版本中目录和文档

 

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

 

应用2,测试git的reset功能

  开发短信-->回退原始-->开发电话-->保留电话并回退一开始的短信功能。

  第一步,开发短信功能

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git commit -m "增加短信功能"
[master 6f9329e] 增加短信功能
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 send_msg

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git log
commit 6f9329e2affb05baa51c8773c6a27fe6484dc291 (HEAD -> master)
Author: fat39 <fat39@163.com>
Date:   Fri Mar 9 14:47:38 2018 +0800

    增加短信功能

commit d91878163e1d748a9c7d1921742fd69151a53f98
Author: fat39 <fat39@163.com>
Date:   Fri Mar 9 14:27:29 2018 +0800

    初次移植到git

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
增加短信功能

   第二步,回退到原始状态

$ git log
commit 6f9329e2affb05baa51c8773c6a27fe6484dc291 (HEAD -> master)
Author: fat39 <fat39@163.com>
Date:   Fri Mar 9 14:47:38 2018 +0800

    增加短信功能

commit d91878163e1d748a9c7d1921742fd69151a53f98
Author: fat39 <fat39@163.com>
Date:   Fri Mar 9 14:27:29 2018 +0800

    初次移植到git

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)



$ git reset --hard d91878163e1d748a9c7d1921742fd69151a53f98
HEAD is now at d918781 初次移植到git
回退

 

  第三步,开发电话功能,并提交git

  第四步,保留电话功能,并找回短信功能

  git reflog
  git reset --mix 版本号
  git checkout 文件名

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git log
commit 07dee36c4ae12368641e67fd707caac8f9c18968 (HEAD -> master)
Author: fat39 <fat39@163.com>
Date:   Fri Mar 9 15:11:47 2018 +0800

    回退后,增加了电话功能

commit d91878163e1d748a9c7d1921742fd69151a53f98
Author: fat39 <fat39@163.com>
Date:   Fri Mar 9 14:27:29 2018 +0800

    初次移植到git

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git reflog
07dee36 (HEAD -> master) HEAD@{0}: commit: 回退后,增加了电话功能
d918781 HEAD@{1}: reset: moving to d91878163e1d748a9c7d1921742fd69151a53f98
6f9329e HEAD@{2}: commit: 增加短信功能
d918781 HEAD@{3}: commit (initial): 初次移植到git

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git reset --mix 6f9329e
Unstaged changes after reset:
D       send_msg

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ 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:    send_msg

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

        phone

no changes added to commit (use "git add" and/or "git commit -a")

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git checkout send_msg
保留电话功能,并找回短信功能

 

 

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

 

应用3,开发新功能的过程中,线上版本出现bug的处理

  线上稳定版本 --> 新开发功能 --> 出现bug,先保存目前开发进度 --> 解决bug --> 继续开发 --> 处理冲突

  (1)branch处理(推荐)

  原程序:master分支

  正在开发:dev分支

  修复bug:bug分支

  第一步,在dev分支开发到一半时,原线上稳定版本遇到bug。dev分支先保存进度。

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (dev)
$ git add .

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (dev)
$ git commit -m "开发到一半"
[dev 99a7855] 开发到一半
 2 files changed, 3 insertions(+), 1 deletion(-)
 create mode 100644 "dev\345\274\200\345\217\221\345\210\260\344\270\200\345\215\212.txt"

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (dev)
$ git status
On branch dev
nothing to commit, working tree clean

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (dev)
$ git log
commit 99a78558a6359e0800cf4abb3a863875c4718928 (HEAD -> dev)
Author: fat39 <fat39@163.com>
Date:   Fri Mar 9 16:58:35 2018 +0800

    开发到一半

commit e30d93e69a522aec633c3036e71d2148bf862d99 (master)
Author: fat39 <fat39@163.com>
Date:   Fri Mar 9 16:55:29 2018 +0800

    线上稳定版本

commit d91878163e1d748a9c7d1921742fd69151a53f98
Author: fat39 <fat39@163.com>
Date:   Fri Mar 9 14:27:29 2018 +0800

    初次移植到git
开发到一半,保存进度。

 

  第二步,切换回master,新增一个bug分支,bug处理完成。切换回master并合并bug。

  说明:合并时可能会遇到两种情况:1、无冲突,顺利合并(这里先设计为无冲突合并);2、冲突,人工合并。(后面的步骤会出现冲突)

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (bug)
$ git status
On branch bug
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        bug.txt

nothing added to commit but untracked files present (use "git add" to track)

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (bug)
$ git add .

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (bug)
$ git commit -m "修复bug"
[bug 57a8774] 修复bug
 1 file changed, 1 insertion(+)
 create mode 100644 bug.txt
修复bug

 

  上线后删除bug分支

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git branch
  bug
  dev
* master

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git branch -d bug
Deleted branch bug (was 57a8774).

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git branch
  dev
* master
上线完成,删除bug分支

 

  第三步,继续开发,合并,处理冲突。

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (dev)
$ git add .

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (dev)
$ git commit -m "开发完成"
[dev 43c9dd7] 开发完成
 2 files changed, 3 insertions(+), 1 deletion(-)
 create mode 100644 "dev\345\274\200\345\217\221\345\256\214\346\210\220.txt"

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (dev)
$ git checkout master
Switched to branch 'master'

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git checkout dev
Switched to branch 'dev'

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (dev)
$ git branch
* dev
  master

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (dev)
$ git checkout master
Switched to branch 'master'

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git merge dev
Auto-merging 稳定2.txt
CONFLICT (content): Merge conflict in 稳定2.txt
Automatic merge failed; fix conflicts and then commit the result.

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master|MERGING)
$
开发完成,处理冲突

  冲突时,master版本仍然是原版本,待冲突处理完成后进行commit上线

$ git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Changes to be committed:

        new file:   "dev\345\274\200\345\217\221\345\210\260\344\270\200\345\215                                                                                            \212.txt"
        new file:   "dev\345\274\200\345\217\221\345\256\214\346\210\220.txt"
        modified:   "\347\250\263\345\256\2321.txt"

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   "\347\250\263\345\256\2322.txt"


fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master|MERGING)
$ git add .

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master|MERGING)
$ git commit -m "处理冲突完成"
[master a5526d1] 处理冲突完成

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git log
commit a5526d175b4a7edd23823b665254d4606949a478 (HEAD -> master)
Merge: d986884 43c9dd7
Author: fat39 <fat39@163.com>
Date:   Fri Mar 9 17:29:29 2018 +0800

    处理冲突完成

commit 43c9dd7da392f69883606cdde2358ecdfd13a4ab (dev)
Author: fat39 <fat39@163.com>
Date:   Fri Mar 9 17:20:15 2018 +0800

    开发完成

commit d986884ffbb0a49a6d2026cd198d78eed516af96
Author: fat39 <fat39@163.com>
Date:   Fri Mar 9 17:18:50 2018 +0800

    修复bug完成

commit af144c3973935ebdafc48eadd01a39697a00a24f
Author: fat39 <fat39@163.com>
Date:   Fri Mar 9 17:16:34 2018 +0800

:
处理冲突完成

   (2)stash处理(不推荐)

  第一步,工作区出现bug,将当前已经做过的修改,保存到一个临时的地方

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ 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:   "\347\250\263\345\256\2321.txt"

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

        "\345\274\200\345\217\221\345\210\260\344\270\200\345\215\212.txt"

no changes added to commit (use "git add" and/or "git commit -a")

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git stash
Saved working directory and index state WIP on master: e30d93e 线上稳定版本
stash放到临时的地方

  第二步,修复bug,产生冲突

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git commit -m "修复bug"
[master 3b147d7] 修复bug
 3 files changed, 5 insertions(+), 4 deletions(-)
 create mode 100644 "\344\277\256\345\244\215bug.txt"
 create mode 100644 "\345\274\200\345\217\221\345\210\260\344\270\200\345\215\212.txt"

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git status
On branch master
nothing to commit, working tree clean

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git stash list
stash@{0}: WIP on master: e30d93e 线上稳定版本

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git stash pop
Auto-merging 稳定1.txt
CONFLICT (content): Merge conflict in 稳定1.txt

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$
修复bug后,把正在开发的拿回来,产生冲突

   第三部,手动解决冲突,继续后续开发

$ git status
On branch master
Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

        both modified:   "\347\250\263\345\256\2321.txt"

no changes added to commit (use "git add" and/or "git commit -a")

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git status
On branch master
Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

        both modified:   "\347\250\263\345\256\2321.txt"

no changes added to commit (use "git add" and/or "git commit -a")

fat39@fat39 MINGW64 /d/Python相关/git/git实验/experiment (master)
$ git log
commit 3b147d77221047982590e84dfccf6e94c7d1fd2d (HEAD -> master)
Author: fat39 <fat39@163.com>
Date:   Fri Mar 9 17:46:18 2018 +0800

    修复bug

commit e30d93e69a522aec633c3036e71d2148bf862d99
Author: fat39 <fat39@163.com>
Date:   Fri Mar 9 16:55:29 2018 +0800

    线上稳定版本

commit d91878163e1d748a9c7d1921742fd69151a53f98
Author: fat39 <fat39@163.com>
Date:   Fri Mar 9 14:27:29 2018 +0800

    初次移植到git
目前commit版本是修复bug后的版本。手动解决冲突,无需commit

 

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

 

应用4、远程仓库

  远程仓库
  公共:
    github
    https://code.csdn.net
    https://git.oschina.net
  公司:

    gitlab:
  

  使用:
    1、注册账号
    2、创建代码仓库 Experiment
  https://github.com/fat39/Experiment.git

 

0、注册账号,新增项目

 

1、第一次,在公司,上传项目到github

  创建远程连接,为地址起一个别名origin

  git remote add origin https://github.com/fat39/Experiment.git

  把master分支推送到github

  git push origin master 

  把其他分支(dev…)推送到github

      git checkout dev

      git push origin dev

2、第一次,到家里,首次下载项目,进行开发,上传到github

        git clone https://github.com/fat39/Experiment.git

  cd Experiment

  git branch # 此时只有master

  git branch -a  # 用-a查看所有分区

  git branch dev origin/dev  # 拉远程分支的代码

  git checkout dev

  在家里写代码

          git add .

          git commit ...

          git push origin dev

 

3、第二次,回到公司,把家里开发的内容拉下来

        git checkout dev

        git pull origin dev  #

  开发了文件1并上传到github,开发了文件2但忘了上传。

        文件1:

                 git add .

                 git commit –m “在公司开发的1,上传到github”

                 git push origin dev

 

4、第二次回到家中,下载公司进度,此时只能pull到文件1,继续开发文件3,并修改了文件1。

        git pull origin dev

        。。。开发。。。

        git add .

        git commit –m “第二次在家开发的,修改了1,增加了3”

        git push origin dev

        在github上看见修改后的文件1和新增的文件3

5、第三次回到公司,pull下来最新版,此时公司pc上可看见未上传的文件2,家里修改后的文件1,家里新增的文件3.

        git pull origin dev

        git status

 五、协同开发

同一个远程仓库

github:
  - 组织
  - 邀请
1.拉代码
2.开发。。。
3.合并
先提交:没问题。

后提交:需要先合并,解决冲突,再提交

实验过程暂缺。

参考别人的:http://www.cnblogs.com/haiyan123/p/7994318.html

 

六、fork别人的项目

1.fork别人的项目(把整个项目单独保存在自己的github)
2.git clone xxxx(本地下载该项目)
3.修改
4.提交【自己】(上传到自己的gihub)
5.pull new request(向项目作者提请求,提交自己的代码,希望其按需求决定是否更新到系统)
6.等

 

posted @ 2018-03-11 19:44  fat39  阅读(204)  评论(0编辑  收藏  举报