多人写作项目中, 需要建立一个主分支, 将可发布的代码提交进来。 日常开发, 个人的代码提交到个人的分支。

1. 在git server 上, 在一个repository中, 可以fork出个人分支, 就是我们自己的workspace

一. 建立本地工作环境

   a. 初始化本地环境

        >git init

  b. git remote 关联远程仓库, 注:自主建立工作目录,不使用clone,属于脑洞大开型,有可能导致本地和远程出现不同步的地方,比如先在本地加入一个文件

>git remote add ly https://github.com/lianghyan/hope.git

删除一个远程仓库关联

>git remote rm ly

  c. 本地没有下载工程的情况下, 自以为是的加入了一些file

   >git add hello.txt

  d.从远程下载分支到本地

>git pull ly master --alow-unrelated-histories

取得远程仓库ly所有的结构信息

>git fetch ly

取得远程仓库ly的分支master的信息

>git fetch ly master

  e. 提交本地的文件到远程server

 >git push -u ly master

二. 将主分支的改动同步到个人分支

1.将本地的project关联到远程的主分支和个人分支

>git remote add mainhope https://github.com/blue/hope.git

>git remote add subhope https://github.com/subblue/hope.git

首先将远程主分支同步到本地

  >git pull mainhope dev

将本地的推送到远程个人分支

>git push subhope dev

>git push ply dev

 

查看当前分支

>git remote add mainhope https://github.com/subblue/hope.git

>git remote add subhope https://github.com/blue/hope.git

>git branch
* master

>git pull mainhope develop

>git push subhope develop

error: src refspec develop does not match any.
error: failed to push some refs to 'https://github.com/blue/hope.git'

>git checkout -b develop
.....
Switched to a new branch 'develop'

>git push subhope develop

 

hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

不想pull 代码的情况下,强行提交

git push -u ly dev -f

放弃本地修改, 强行使用服务器分支替代本地分支

git checkout develop

git reset --hard ly/develop

 

 Git 回退

>git log

>git reset --hard id

>git reset --hard 9c30bb3cfee78246fbdf18b4ff860a2b0830c5ce

 

Please commit your changes or stash them before you switch branches.

  • git stash

  • git pull

  • git stash pop

Merge one commit from branch dev to branch uat

1. git checkout dev

2. git log

3. find the [commit-id] that needs to be merged into uat 

4. git pull ly uat

5.  git checkout uat

6. git cherry-pick [commit-id]

7. git push ly uat

 

fatal: bad revision:

reason: the commit doesn't exist on your local, execute 'git fetch' to get the branch.

>git cherry-pick xxxx
fatal: bad revision 'xxx'
>git fetch --all
>git cherry-pick ad25571
>git push

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐