重工电子论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 6736|回复: 2
打印 上一主题 下一主题

Github进阶教程--提交stm32工程

[复制链接]

15

主题

24

帖子

379

积分

学生管理组

Rank: 8Rank: 8

积分
379
跳转到指定楼层
楼主
发表于 2017-2-10 21:33:13 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 刘鑫-15电子 于 2017-2-11 22:00 编辑

把之前的步骤走过一次之后,应该对于Github有一个基本的了解了。在这里,我还是要把这个教程发出来  http://wiki.jikexueyuan.com/project/pro-git-two/get-repository.html  现在看这里面的东西,估计能看明白了。(刚刚那个帖子没经验,写得太丑,所以我重新建一个帖子)。
我现在来演示一下,真正我们平时会用到的一些操作。这次我会以stm32的一个跑马灯程序做演示。
首先,我们在远程端新建了一个名为LED的仓库               
             。
可以看到,这是一个空的项目,里面什么都没有,我们将上传一个stm32的工程上来,并对其进行操作,先复制它的url。回到本地, ,我们这里有个LED的工程,里面有项目文件夹。点进去,git bash .弹出git 命令窗口。


接下来,我们输入 git clone https://github.com/cqut-liuxin/LED.git     git clone 后面黏贴 url

[PowerShell] syntaxhighlighter_viewsource syntaxhighlighter_copycode
xin@XIN MINGW64 ~/Desktop/LED
$ git clone [url=https://github.com/cqut-liuxin/LED.git]https://github.com/cqut-liuxin/LED.git[/url]
Cloning into 'LED'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.

xin@XIN MINGW64 ~/Desktop/LED[/color][/size][size=4][color=#000000]$

以后,我们在项目根目录下得到一个LED的文件夹,这个就是远程仓库克隆到本地的仓库。   
进入文件夹,把里面的所有东西拷贝出来。(实际上只有一个名为 “.git”的隐藏文件夹。或者你在建立远程仓库的勾选了如图中这个东西。就会有一个README文件)

把里面的都拷贝出来到上层目录,然后删除 clone 下来的这个文件夹。
现在可以提交了吗?
不,stm32当中,总会产生一些过程文件,比如OBJ文件夹里面的东西,这些是我们用不到的,我们可以将它删除,Github提供了这样的一种方式。我们可以建立一个名单,将你不需要上传的东西,不标记,不提交,不上传,称这些为忽略文件。(这个一定要在第一次上传的时候就做好)
这个名单的建立如下:在git bash 里输入
[Shell] syntaxhighlighter_viewsource syntaxhighlighter_copycode
xin@XIN MINGW64 ~/Desktop/LED
$ touch .gitignore

xin@XIN MINGW64 ~/Desktop/LED (master)
$

这是Linux建立文件的语句。dos一样可以用。

我们可以看到,在目录里建立了一个名为 “.gitigonre”的无后缀文件。 我们也可以自己在外面建立。右键->新建文本->命名为“.gitignore.”,注意,没有后缀哦,最后enter就发现建立个无后缀文件。这就是dos环境下建立无后缀文件的方法
这里,我介绍下编辑规则。
具体详细的规则见http://wiki.jikexueyuan.com/proj ... -to-repository.html “ 忽略文件”。我们用记事本或者notepad++之类的文件编辑器,也可以用vim指令(这里不做介绍,大家自己去看,我反正用不惯)来编辑一个这样的忽略文件列表。


我写的就是一个凡是以a或o为后缀的文件都忽略。为了显示效果,我在OBJ文件夹下,建立了一个名为“ null.a”的文件。
之后,我们就可以进行开始提交的操作了,“ git add . ” 标记跟踪所有文件
[Shell] syntaxhighlighter_viewsource syntaxhighlighter_copycode
xin@XIN MINGW64 ~/Desktop/LED (master)
$ git add .
warning: LF will be replaced by CRLF in USER/LED.uvguix.Administrator.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in USER/LED.uvguix.LIUXIN.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in USER/LED.uvguix.PH.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in USER/LED.uvguix.刘鑫.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in USER/LED.uvoptx.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in USER/LED.uvprojx.
The file will have its original line endings in your working directory.




之后  我们输入 git status   这个指令的意思就是,查看现在文件的状态。
[Perl] syntaxhighlighter_viewsource syntaxhighlighter_copycode
[/size][size=4]xin@XIN MINGW64 ~/Desktop/LED (master)
$ git status
On branch master

Initial commit

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

        new file:   .gitignore
        new file:   CORE/core_cm3.c
        new file:   CORE/core_cm3.h
        new file:   CORE/startup_stm32f10x_hd.s
        new file:   HARDWARE/LED/led.c
        new file:   HARDWARE/LED/led.h
        new file:   README.TXT
        new file:   STM32F10x_FWLib/inc/misc.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_adc.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_bkp.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_can.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_cec.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_crc.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_dac.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_dbgmcu.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_dma.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_exti.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_flash.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_fsmc.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_gpio.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_i2c.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_iwdg.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_pwr.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_rcc.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_rtc.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_sdio.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_spi.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_tim.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_usart.h
        new file:   STM32F10x_FWLib/inc/stm32f10x_wwdg.h
        new file:   STM32F10x_FWLib/src/misc.c
        new file:   STM32F10x_FWLib/src/stm32f10x_adc.c
        new file:   STM32F10x_FWLib/src/stm32f10x_bkp.c
        new file:   STM32F10x_FWLib/src/stm32f10x_can.c
        new file:   STM32F10x_FWLib/src/stm32f10x_cec.c
        new file:   STM32F10x_FWLib/src/stm32f10x_crc.c
        new file:   STM32F10x_FWLib/src/stm32f10x_dac.c
        new file:   STM32F10x_FWLib/src/stm32f10x_dbgmcu.c
        new file:   STM32F10x_FWLib/src/stm32f10x_dma.c
        new file:   STM32F10x_FWLib/src/stm32f10x_exti.c
        new file:   STM32F10x_FWLib/src/stm32f10x_flash.c
        new file:   STM32F10x_FWLib/src/stm32f10x_fsmc.c
        new file:   STM32F10x_FWLib/src/stm32f10x_gpio.c
        new file:   STM32F10x_FWLib/src/stm32f10x_i2c.c
        new file:   STM32F10x_FWLib/src/stm32f10x_iwdg.c
        new file:   STM32F10x_FWLib/src/stm32f10x_pwr.c
        new file:   STM32F10x_FWLib/src/stm32f10x_rcc.c
        new file:   STM32F10x_FWLib/src/stm32f10x_rtc.c
        new file:   STM32F10x_FWLib/src/stm32f10x_sdio.c
        new file:   STM32F10x_FWLib/src/stm32f10x_spi.c
        new file:   STM32F10x_FWLib/src/stm32f10x_tim.c
        new file:   STM32F10x_FWLib/src/stm32f10x_usart.c
        new file:   STM32F10x_FWLib/src/stm32f10x_wwdg.c
        new file:   SYSTEM/delay/delay.c
        new file:   SYSTEM/delay/delay.h
        new file:   SYSTEM/sys/sys.c
        new file:   SYSTEM/sys/sys.h
        new file:   SYSTEM/usart/usart.c
        new file:   SYSTEM/usart/usart.h
        new file:   USER/JLinkSettings.ini
        new file:   USER/LED.uvguix.Administrator
        new file:   USER/LED.uvguix.LIUXIN
        new file:   USER/LED.uvguix.PH
        new file:   "USER/LED.uvguix.\345\210\230\351\221\253"
        new file:   USER/LED.uvoptx
        new file:   USER/LED.uvprojx
        new file:   USER/main.c
        new file:   USER/stm32f10x.h
        new file:   USER/stm32f10x_conf.h
        new file:   USER/stm32f10x_it.c
        new file:   USER/stm32f10x_it.h
        new file:   USER/system_stm32f10x.c
        new file:   USER/system_stm32f10x.h
        new file:   keilkilll.bat

xin@XIN MINGW64 ~/Desktop/LED (master)

现在,我们可以看到,要提交的文件中,都变绿了。,都已被标记,我们发现,并没有找到null.a 因为它被忽略了。接下来,我们commit至本地记录(看了资料的应该知道)。
[Shell] syntaxhighlighter_viewsource syntaxhighlighter_copycode
xin@XIN MINGW64 ~/Desktop/LED (master)
$ git commit -m "add a new project named LED"
[master (root-commit) eeb24cf] add a new project named LED
warning: LF will be replaced by CRLF in USER/LED.uvguix.Administrator.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in USER/LED.uvguix.LIUXIN.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in USER/LED.uvguix.PH.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in USER/LED.uvguix.刘鑫.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in USER/LED.uvoptx.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in USER/LED.uvprojx.
The file will have its original line endings in your working directory.
74 files changed, 47425 insertions(+)
create mode 100644 .gitignore
create mode 100644 CORE/core_cm3.c
create mode 100644 CORE/core_cm3.h
create mode 100644 CORE/startup_stm32f10x_hd.s
create mode 100644 HARDWARE/LED/led.c
create mode 100644 HARDWARE/LED/led.h
create mode 100644 README.TXT
create mode 100644 STM32F10x_FWLib/inc/misc.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_adc.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_bkp.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_can.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_cec.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_crc.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_dac.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_dbgmcu.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_dma.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_exti.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_flash.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_fsmc.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_gpio.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_i2c.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_iwdg.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_pwr.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_rcc.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_rtc.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_sdio.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_spi.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_tim.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_usart.h
create mode 100644 STM32F10x_FWLib/inc/stm32f10x_wwdg.h
create mode 100644 STM32F10x_FWLib/src/misc.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_adc.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_bkp.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_can.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_cec.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_crc.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_dac.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_dbgmcu.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_dma.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_exti.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_flash.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_fsmc.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_gpio.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_i2c.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_iwdg.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_pwr.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_rcc.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_rtc.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_sdio.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_spi.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_tim.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_usart.c
create mode 100644 STM32F10x_FWLib/src/stm32f10x_wwdg.c
create mode 100644 SYSTEM/delay/delay.c
create mode 100644 SYSTEM/delay/delay.h
create mode 100644 SYSTEM/sys/sys.c
create mode 100644 SYSTEM/sys/sys.h
create mode 100644 SYSTEM/usart/usart.c
create mode 100644 SYSTEM/usart/usart.h
create mode 100644 USER/JLinkSettings.ini
create mode 100644 USER/LED.uvguix.Administrator
create mode 100644 USER/LED.uvguix.LIUXIN
create mode 100644 USER/LED.uvguix.PH
create mode 100644 "USER/LED.uvguix.\345\210\230\351\221\253"
create mode 100644 USER/LED.uvoptx
create mode 100644 USER/LED.uvprojx
create mode 100644 USER/main.c
create mode 100644 USER/stm32f10x.h
create mode 100644 USER/stm32f10x_conf.h
create mode 100644 USER/stm32f10x_it.c
create mode 100644 USER/stm32f10x_it.h
create mode 100644 USER/system_stm32f10x.c
create mode 100644 USER/system_stm32f10x.h
create mode 100644 keilkilll.bat


git push -u origin master    上传服务器,我们选择的分支还是master(我在这里解释一下  git push -u 是固定格式 后面跟url 和 分支名称,origin就是url的替代,master就是默认的分支名称。在默认分支下,我们也可以直接输入git push  完成上传。)   

[Shell] syntaxhighlighter_viewsource syntaxhighlighter_copycode
xin@XIN MINGW64 ~/Desktop/LED (master[/size]
[size=4]$ git push -u origin master[/size]
[size=4]Counting objects: 72, done.[/size]
[size=4]Delta compression using up to 4 threads.[/size]
[size=4]Compressing objects: 100% (70/70), done.[/size]
[size=4]Writing objects: 100% (72/72), 191.90 KiB | 0 bytes/s, done.[/size]
[size=4]Total 72 (delta 11), reused 0 (delta 0)[/size]
[size=4]remote: Resolving deltas: 100% (11/11), done.[/size]
[size=4]To [url=https://github.com/cqut-liuxin/LED.git]https://github.com/cqut-liuxin/LED.git[/url][/size]
[size=4]* [new branch]      master -> master[/size]
[size=4]Branch master set up to track remote branch master from origin.[/size]
[size=4]$ git push -u origin master[/size]
[size=4]Counting objects: 72, done.[/size]
[size=4]Delta compression using up to 4 threads.[/size]
[size=4]Compressing objects: 100% (70/70), done.[/size]
[size=4]Writing objects: 100% (72/72), 191.90 KiB | 0 bytes/s, done.[/size]
[size=4]Total 72 (delta 11), reused 0 (delta 0)[/size]
[size=4]remote: Resolving deltas: 100% (11/11), done.[/size]
[size=4]To [url=https://github.com/cqut-liuxin/LED.git]https://github.com/cqut-liuxin/LED.git[/url][/size]
[size=4]* [new branch]      master -> master[/size]
[size=4]Branch master set up to track remote branch master from origin.[/size]
[size=4]

上传完成,如果失败,多是两次试试,如果还不行,就要仔细去看错误原因 。

关于仓库的更新:我们这里先只讲一个分支的情况
如果有人提交了新的版本,那我们怎么更新我们本地的仓库呢,我们现在做如下的两个指令的演示git fetch 、git merge  以及git pull  其实想要真的认识这个东西,需要去了解git的工作方式,几个区:工作区(workspace)、暂存区(index)、本地仓库(local repository),当然还有远程仓库(remote repository)。 以及还有branch (分支)的概念。这个概念比较强大,我们其实知道怎么用就可以了。
首先,我们用另外的账号,或者在其他地方提交了一个新的版本。我还是拿我这个LED的工程为例。
我现在在另一个地方对远程仓库进行了一个修改。修改如下:删除了keilkill.bat  和 README 增加了 new.txt


那么我们现在本地的那个LED是不是就不是最新的了?
那么,我们难道还要重新克隆一次?当然不是。我们只需"刷新"就好了。(这个刷新不是真的叫你刷新。。。)
我们看,本地的情况,没有更新。现在在工作目录下,右键git bash .

然后输入 git fetch.
[Shell] syntaxhighlighter_viewsource syntaxhighlighter_copycode
xin@XIN MINGW64 ~/Desktop/LED (master)[/size]
[size=4]$ git fetch[/size]
[size=4]remote: Counting objects: 3, done.[/size]
[size=4]remote: Compressing objects: 100% (1/1), done.[/size]
[size=4]remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0[/size]
[size=4]Unpacking objects: 100% (3/3), done.[/size]
[size=4]From [/size][size=4][url=https://github.com/cqut-liuxin/LED]https://github.com/cqut-liuxin/LED[/url][/size]
[size=4]   4e3b834..93c5c18  master     -> origin/master[/size]

[size=4]xin@XIN MINGW64 ~/Desktop/LED (master)[/size]
[size=4]$[/size]
[size=4]


我们一看,呀,没变呀!一个没多一个没少。我也是懵逼的,最后我一看,在.git 文件夹下多了一个FETCH_HEAD。。。其实,这个git fetch 已经起了作用,它的作用就是,把最新的版本的连接存在了这个文件里面。之前看过教程的应该知道,实际上,github 与其他版本控制比如svn的区别在于,github实际上储存的是一系列文件快照。。。。。。balabala。实际上我也没用过svn,也不知道区别在哪里,但是意思就是,github会把每个版本的文件指针存下来,而不是去存改动,现在我们已经有了,只需要把它弄出来,搞到本地的工作目录。。
我们执行git merge ,这个指令的作用就是,把  FETCH_HEAD里的文件合并到当前。
[Shell] syntaxhighlighter_viewsource syntaxhighlighter_copycode
xin@XIN MINGW64 ~/Desktop/LED (master)[/size]
[size=4]$ git merge[/size]
[size=4]Updating 4e3b834..93c5c18[/size]
[size=4]Fast-forward[/size]
[size=4] README.TXT    | 60 -----------------------------------------------------------[/size]
[size=4] keilkilll.bat | 27 ---------------------------[/size]
[size=4] new.txt       |  0[/size]
[size=4] 3 files changed, 87 deletions(-)[/size]
[size=4] delete mode 100644 README.TXT[/size]
[size=4] delete mode 100644 keilkilll.bat[/size]
[size=4] create mode 100644 new.txt[/size]
[size=4]


执行完之后,看文件夹,,是不是就有了改变。

那么git pull指令   其实就是两个指令的综合,一步到位。我们在每次更新代码之前,一定要先看看我们当前版本是不是最新的,否则,在提交的时候,就会出现冲突。
注意:这几个指令实际上都是要加分支名参数的,但是我之前就说了,我们当前只有一个分支master,所以我们现在不带参数也是可以的。下面,我来讲解 如何进行版本还原。
有时候,我们突然需要上一个版本甚至最初版本的代码吗,那么怎么办,这又是github的一大作用。我们来试试,还原到上一个版本。
我们先来查询,我们都有哪些版本。
git log 这条指令也是有很多用处,根据后面跟的参数,有不同的作用。我们这里,不带任何东西,作用就是可以查询到所有的commit 历史,
[Shell] syntaxhighlighter_viewsource syntaxhighlighter_copycode
xin@XIN MINGW64 ~/Desktop/LED (master)[/size]
[size=4]$ git log[/size]
[size=4]commit 93c5c18ccaf6cee7e10d78665f7555054eceb9d9[/size]
[size=4]Author: cqut-liuxin <[/size][size=4][url=mailto:625092890@qq.com]625092890@qq.com[/url][/size][size=4]>[/size]
[size=4]Date:   Sat Feb 11 13:54:39 2017 +0800[/size]

[size=4]    delete and delete[/size]

[size=4]    delete akeilkill,README  and add new[/size]

[size=4]commit 4e3b834b65ec406c7250f2eebeba33a4172a4d9e[/size]
[size=4]Author: cqut-liuxin <[/size][size=4][url=mailto:625092890@qq.com]625092890@qq.com[/url][/size][size=4]>[/size]
[size=4]Date:   Sat Feb 11 13:46:01 2017 +0800[/size]

[size=4]    add user[/size]

[size=4]commit 3856ea85a2c73bb2395c15059eec2cc3811a8e9c[/size]
[size=4]Author: cqut-liuxin <[/size][size=4][url=mailto:625092890@qq.com]625092890@qq.com[/url][/size][size=4]>[/size]
[size=4]Date:   Sat Feb 11 13:40:23 2017 +0800[/size]

[size=4]    Revert "d"[/size]

[size=4]    This reverts commit 83a94aa9d8955a85905e0762bb18383e1c3aa5f6.[/size]

[size=4]commit 13626fcb47e2c2652457a77b7931907955d3582c[/size]
[size=4]

英文输入状态下,按q就可以退出列表我们观察这个列表,我们的记录全在那里了,每一次记录都有版本号,Author 、时间、备注,最上面那次是不是备注为“delete akeilkill,README and add new”的这一次,也就是我们当前的版本。对吧?
那么我们如果要返回上一个版本,就找到比它还要上一次提交那个时候的版本号 (commit后面的一长串16进制) 我的是跟你们不一样,每个人都不一样 。以自己的为准。
git reset --hard <版本号>
[Shell] syntaxhighlighter_viewsource syntaxhighlighter_copycode
xin@XIN MINGW64 ~/Desktop/LED (master)[/size]
[size=4]$ git reset --hard 4e3b834b65ec406c7250f2eebeba33a4172a4d9e[/size]
[size=4]HEAD is now at 4e3b834 add user[/size]
[size=4]



好了,去看文件夹,是不是之前删除的文件又回来了,new文件夹不在了?


关于git log 、git reset 还有很多作用,需要你们自己去了解。我把我找到资源留下来 :http://lib.csdn.net/article/git/8677

我们现在以及掌握了建立、提交、更新、还原。那么现在已经可以进行最基本的使用了。这些都是客户端能做的,那么接下来,我就要来演示演示,出现了问题的情况。

情况一 :当前版本不是最新版本修改所得,无法提交。我画个图解释。


A:最初版本   
B1:由A版本修改而来    B2:由A版本修改而来
( B1先提交 B2后提交)
B1提交之后,服务器上最新就是B1版本,如果,你在这个时候提交B2,那么就会出现冲突。我们来试一试。

接着刚刚的例子,我们现在是不是已经会退到上一个版本了?也就变成图中的A ,但是现在,服务器上的是不是还是没变?也就相当于B1.  我们现在做一个修改。
在此之前,我们先来看一下,目前本地的状态和服务器上的状态
本地文件夹:
      
远程仓库                 
        


我们把本地文件 “README .TXT" 里面的内容删了,重新编辑。
改动前:



改动后:


我们现在开始进行
add  commit   push上传试试。
git status 查看状态
[Shell] syntaxhighlighter_viewsource syntaxhighlighter_copycode
xin@XIN MINGW64 ~/Desktop/LED (master)[/size]
[size=4]$ git status[/size]
[size=4]On branch master[/size]
[size=4]Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.[/size]
[size=4]  (use "git pull" to update your local branch)[/size]
[size=4]Changes not staged for commit:[/size]
[size=4]  (use "git add <file>..." to update what will be committed)[/size]
[size=4]  (use "git checkout -- <file>..." to discard changes in working directory)[/size]

[size=4]        modified:   README.TXT[/size]

[size=4]no changes added to commit (use "git add" and/or "git commit -a")[/size]
[size=4]


有了,有一个修改的文件。
git add .
git commit -m "备注"
git push -u origin master
[Shell] syntaxhighlighter_viewsource syntaxhighlighter_copycode
xin@XIN MINGW64 ~/Desktop/LED (master)[/size]
[size=4]$ git status[/size]
[size=4]On branch master[/size]
[size=4]Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.[/size]
[size=4]  (use "git pull" to update your local branch)[/size]
[size=4]Changes not staged for commit:[/size]
[size=4]  (use "git add <file>..." to update what will be committed)[/size]
[size=4]  (use "git checkout -- <file>..." to discard changes in working directory)[/size]

[size=4]        modified:   README.TXT[/size]

[size=4]no changes added to commit (use "git add" and/or "git commit -a")[/size]

[size=4]xin@XIN MINGW64 ~/Desktop/LED (master)[/size]
[size=4]$ git add .[/size]

[size=4]xin@XIN MINGW64 ~/Desktop/LED (master)[/size]
[size=4]$ git status[/size]
[size=4]On branch master[/size]
[size=4]Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.[/size]
[size=4]  (use "git pull" to update your local branch)[/size]
[size=4]Changes to be committed:[/size]
[size=4]  (use "git reset HEAD <file>..." to unstage)[/size]

[size=4]        modified:   README.TXT[/size]


[size=4]xin@XIN MINGW64 ~/Desktop/LED (master)[/size]
[size=4]$ gut commit -m "修改了一个txt文件"[/size]
[size=4]bash: gut: command not found[/size]

[size=4]xin@XIN MINGW64 ~/Desktop/LED (master)[/size]
[size=4]$ git commit -m "修改了一个txt文件"[/size]
[size=4][master ad0e614] 修改了一个txt文件[/size]
[size=4] 1 file changed, 18 insertions(+), 60 deletions(-)[/size]
[size=4] rewrite README.TXT (94%)[/size]

[size=4]xin@XIN MINGW64 ~/Desktop/LED (master)[/size]
[size=4]$ git push -u origin master[/size]
[size=4]To [/size][size=4][url=https://github.com/cqut-liuxin/LED.git]https://github.com/cqut-liuxin/LED.git[/url][/size]
[size=4] ! [rejected]        master -> master (non-fast-forward)[/size]
[size=4]error: failed to push some refs to 'https://github.com/cqut-liuxin/LED.git'[/size]
[size=4]hint: Updates were rejected because the tip of your current branch is behind[/size]
[size=4]hint: its remote counterpart. Integrate the remote changes (e.g.[/size]
[size=4]hint: 'git pull ...') before pushing again.[/size]
[size=4]hint: See the 'Note about fast-forwards' in 'git push --help' for details.[/size]
[size=4]



我们发现,提交失败了。翻译这段话,英语不好的自己有道。我是有道的。所以说,学习git 可以长英语水平。
我们来解释一下
第一句,Updates were rejected because the tip of your current branch is behind its remote counterpart :当前更新被驳回,因为你当前的分支落后于远程仓库
第二句,.Integrate the remote changes (e.g. 'git pull ...') before pushing again.  在push之前可以融合远程版本到本地,例如 git pull
第三局,具体看git push --help    (输入git push --help可以查看帮助,可以去看看)
我们直接给出方案。
1,强推,即利用强覆盖方式用你本地的代码替代git仓库内的内容
          git push -f
2,先把git的东西fetch到你本地然后merge(等效于git pull) ,  后再push

一般吧,如果最新(也就是B1)不是自己提交的,就不要强推了,不然会被打死。
我们采用第二种
试试嘛,,,学习就是多试试。
直接输入git pull 进行融合
[Shell] syntaxhighlighter_viewsource syntaxhighlighter_copycode
$ git pull[/size]
[size=4]Updating 4e3b834..93c5c18[/size]
[size=4]Fast-forward[/size]
[size=4] README.TXT    | 60 -----------------------------------------------------------[/size]
[size=4] keilkilll.bat | 27 ---------------------------[/size]
[size=4] new.txt       |  0[/size]
[size=4] 3 files changed, 87 deletions(-)[/size]
[size=4] delete mode 100644 README.TXT[/size]
[size=4] delete mode 100644 keilkilll.bat[/size]
[size=4] create mode 100644 new.txt[/size]

[size=4]


结果发现,直接被同化成了远程仓库的版本。keilkill.bat 和 README被删除了

我们回到上一个版本,也就是回到A,我们没修改“READ.TXT”的时候。

依旧使用git log  git reset --hard <>

我们做这样的尝试,我们建立一个服务器上没有,历史版本上也没有的文件,比如在文件夹下随便新建一个c文件"   GPIO.c” ,我们在里面写一些代码我们来测试

  


我们再 git add .  然后commit        最后  pull  (注意:前面两部必须要有,因为被标记之后,我们才能进行pull融合)

[Shell] syntaxhighlighter_viewsource syntaxhighlighter_copycode
xin@XIN MINGW64 ~/Desktop/LED (master)
$ git status
On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)
Untracked files:
  (use "git add <file>..." to include in what will be committed)


        GPIO.c


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


xin@XIN MINGW64 ~/Desktop/LED (master)
$ git add .


xin@XIN MINGW64 ~/Desktop/LED (master)
$ git commit -m "新建了一个C文件"
[master 6b9810b] 新建了一个C文件
1 file changed, 6 insertions(+)
create mode 100644 GPIO.c


xin@XIN MINGW64 ~/Desktop/LED (master)
$ git pull
Removing keilkilll.bat
Removing README.TXT
Merge made by the 'recursive' strategy.
README.TXT    | 60 -----------------------------------------------------------
keilkilll.bat | 27 ---------------------------
new.txt       |  0
3 files changed, 87 deletions(-)
delete mode 100644 README.TXT
delete mode 100644 keilkilll.bat
create mode 100644 new.txt



xin@XIN MINGW64 ~/Desktop/LED (master)


我们发现,新建的c文件并没有被清除,由此得出结论,对于新建文件来说,并不会在融合的时候被删除,而对于远程仓库中新版本删除的,但是本地版本还没删除的这些文件来说,在pull的时候会被删除。

接下来,我们再次push
[Shell] syntaxhighlighter_viewsource syntaxhighlighter_copycode
xin@XIN MINGW64 ~/Desktop/LED (master)
$ git push
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 610 bytes | 0 bytes/s, done.
Total 5 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local objects.
To [url=https://github.com/cqut-liuxin/LED.git]https://github.com/cqut-liuxin/LED.git[/url]
   93c5c18..0408807  master -> master

查看远程仓库

push 成功。


讲解了关于文件冲突,我们现在来看代码冲突的情况
我在另外的电脑上,更新了GPIO.c 的代码,并将它上传到了远程仓库。
远程代码仓库里的代码,我截图。


简单的改了一下,并增加了一条语句。
那么,我现在如果在我这个地方的本地,也就是老版本基础上更改的话,那么又冲突了,我们来看我本地的代码。



现在我们进行push和pull。直接push是不成功的。我们依旧pull(不直接覆盖提交)
[Shell] syntaxhighlighter_viewsource syntaxhighlighter_copycode
xin@XIN MINGW64 ~/Desktop/LED (master)
$ git add .

xin@XIN MINGW64 ~/Desktop/LED (master)
$ git  status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   GPIO.c


xin@XIN MINGW64 ~/Desktop/LED (master)
$ git commit -m "改变高低电平"
[master 28db7d9] 改变高低电平
 1 file changed, 3 insertions(+), 1 deletion(-)

xin@XIN MINGW64 ~/Desktop/LED (master)
$ git push
To [url=https://github.com/cqut-liuxin/LED.git]https://github.com/cqut-liuxin/LED.git[/url]
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/cqut-liuxin/LED.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

xin@XIN MINGW64 ~/Desktop/LED (master)
$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From [url=https://github.com/cqut-liuxin/LED]https://github.com/cqut-liuxin/LED[/url]
   0408807..6200b7c  master     -> origin/master
Auto-merging GPIO.c
CONFLICT (content): Merge conflict in GPIO.c
Automatic merge failed; fix conflicts and then commit the result.

xin@XIN MINGW64 ~/Desktop/LED (master|MERGING)
$


跟上面的过程,我们看出了,push  依旧是失败,请求我们pull,我们pull
然后成功了,我们来看GPIO.c 里面的代码情况。


Git  已经为我们做好了标识  哪些地不一样,我们只需要把我们不需要的删掉,然后再进行一次git push 的完整流程就成功了。
[Shell] syntaxhighlighter_viewsource syntaxhighlighter_copycode
xin@XIN MINGW64 ~/Desktop/LED (master|MERGING)
$ git add .

xin@XIN MINGW64 ~/Desktop/LED (master|MERGING)
$ git commit -m "改变了LED的高低电平"
[master b0ba0ba] 改变了LED的高低电平

xin@XIN MINGW64 ~/Desktop/LED (master)
$ git commit -m "改变了LED的高低点平,并增加了对LED3的操作"
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean

xin@XIN MINGW64 ~/Desktop/LED (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean

xin@XIN MINGW64 ~/Desktop/LED (master)
$ git push
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 648 bytes | 0 bytes/s, done.
Total 6 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 1 local objects.
To [url=https://github.com/cqut-liuxin/LED.git]https://github.com/cqut-liuxin/LED.git[/url]
   6200b7c..b0ba0ba  master -> master


push 成功。
我们查看远程仓库中的代码情况。


已经按我们的目的修改了。

到这里,我的基础教程就已经搞定了。对于Github  更重要的关于分支的操作,以及一台电脑多账户的管理,还有组织、team的创建与管理,我有时间再写一个帖子。写这个帖子其实本着,反正都要学,干脆出个教程给后面的学弟学妹,第一个是让大家了解这个东西(像Github,如果不是强哥介绍,我或许到工作之前都不会知道。所以我觉得有这个必要发出来,本科阶段,始终见识不足。),第二个希望可以对实验室一起做项目的,带来一个便捷。因为我是边学边写,很多地方可能都存在问题,所以,看的时候,一定要结合我最初发出来的那个极客学院的教程!








分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

299

主题

684

帖子

7026

积分

学生管理组

Rank: 8Rank: 8

积分
7026
沙发
发表于 2020-7-22 09:22:29 | 只看该作者
回复 支持 反对

使用道具 举报

299

主题

684

帖子

7026

积分

学生管理组

Rank: 8Rank: 8

积分
7026
板凳
发表于 2020-8-12 16:02:51 | 只看该作者
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|cqutlab ( 渝ICP备15004556号

GMT+8, 2024-12-22 22:52 , Processed in 0.173066 second(s), 30 queries .

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表