git & github 同步文件

step1 : 在github上建立一个 repository

step2 : clone 到本地

$ git clone https://github.com/ntu-juking/softwaretesting.git

step3 :本地修改文件,添加文件等

$  cd softwaretesting
$  nano README.md ******  // 编辑文件README.md
$  git add README.md  ******// 可能不是必须的
$  git commit -m "Charpter 1 Modify"  // 本地提交 commit

step4:建立远端(github)仓库的别名, 把本地仓库关联远程仓库

$  git remote add origin_RemoteAlias https://github.com/ntu-juking/softwaretesting.git

step5: 本地仓库某分支push 到远端仓库

$  git push origin_RemoteAlias  master         ***// origin_RemoteAlias  远端仓库别名,任意取, 一般是origin。
                                        ***********************//   master := 当前分支名**。

step5':远端仓库 pull 到本地仓库

$  git pull +origin(远程名字)+master(远端分支名):branch_local(本地分支,缺省为当前分支)
  • 把远程仓库最新版本放到本地仓库 (如果没有冲突,则正确执行)

同步冲突解决办法

  • 如果上次提交之后,在服务器端、本地端 都分别修改了对应分支下的文件,则会发生冲突
$ git pull origin mindmap

错误信息如下:
From https://github.com/ntu-juking/softwaretesting
* branch mindmap -> FETCH_HEAD
error: Your local changes to the following files would be overwritten by merge:
mindmap/Lec07_Exp_PerformanceTesting.md
Please commit your changes or stash them before you merge.
Aborting

解决办法:

 $ git mergetool ,执行冲突解决(一般是代码行比对,选择执行哪一个修改)
 $ git commit -a -m "merge conflict fixing" , 提交修改
 $ git push,       合并修改后,提交到远端服务器

实验1: 测试修改多个文件、子路径下文件、提交、push 到远端

  • 我们建立一个分支(mindmap_local,本地分支用于介绍各个讲座的知识要点)
  • 分别用Lec00_name.md, Lec01_name.md, ...... , LecNN_name.md, 为了便于管理,新建一个文件夹(不妨叫mindmap),把这些文件置于文件夹mindmap中
 $  git add   .                    ******************************************************   // 添加到本地库**
 $  git commit -m "add mindmaps"          ***********************************  // 提交变化到本地卡**
 $  git push origin(远端库)  mindmap(远端分支):mindmap_local(本地分支,可省略)   ***********// push 到github库**
原文地址:https://www.cnblogs.com/juking/p/7079646.html