简体   繁体   English

git push保存历史和分支

[英]git push preserving history and branches

I have a local repository I've created with 'git init' and started making commits. 我有一个用'git init'创建的本地存储库,并开始进行提交。 A 'remote' repository has been created for my work on some server and now I need to 'update' it with my entire work (preserving history and branches). 已经为我在某些服务器上的工作创建了一个“远程”存储库,现在我需要使用整个工作来“更新”它(保留历史记录和分支)。

I assume I need to first clone that 'remote' into my machine, then 'update' it somehow to hold the entire historical content and then push, but: 1. - How do I do that? 我假设我需要首先将“远程”克隆到计算机中,然后以某种方式“更新”它以保存整个历史内容,然后推送,但是:1.-我该怎么做? 2. - Is there a simple way to do that (meaning 'automatically' - so that git does it all for me)? 2.-有没有简单的方法可以做到这一点(意思是“自动”-以便git为我完成全部工作)?

Cheers 干杯

Now I realised there is already "something" in that 'remote' repository, meaning local repository shall be 'pushed' as a branch of 'remote', so that my 'master' branch becomes a 'someName' branch on 'remote', again - preserving all history. 现在我意识到该“远程”存储库中已经存在“某物”,这意味着本地存储库将被“推”为“远程”的一个分支,这样我的“主”分支就变成了“远程”上的“ someName”分支,再次-保存所有历史。

That is the issue: you cannot just push, especially if the master branch has already some history. 这就是问题所在:您不能仅仅进行推送,尤其是在master分支已经具有一些历史记录的情况下。

If your commits add to the existing content, what you would do is replay them on top of the existing remote history: 如果您的提交添加到现有内容中,那么您将在现有远程历史记录的顶部重播它们:

cd /path/to/local/repo
git checkout master
git remote add origin /url/remote/repo
git fetch
git rebase origin/master
git push -u origin master    

If the history is not related to the existing content, then it is easier to create an empty repo on the server side. 如果历史记录与现有内容不相关,则在服务器端创建空存储库会更容易。
From there, a simple git push --mirror will push everything (all branches) 从那里开始,简单的git push --mirror将推送所有内容 (所有分支)

there is already "something" in that 'remote' repository, meaning local repository shall be 'pushed' as a branch of 'remote', so that my 'master' branch becomes a 'someName' branch on 'remote', again - preserving all history. 该“远程”存储库中已经存在“某物”,这意味着应将本地存储库“推”为“远程”的一个分支,以便我的“主”分支再次成为“远程”上的“ someName”分支。所有的历史。

So the simplest way to solve your problem is to create a new branch with any name in your remote repo and push your local working copy's changes to that branch. 因此,解决问题的最简单方法是使用远程仓库中的任何名称创建一个新分支,并将本地工作副本的更改推送到该分支。

Simplest way to achieve is as follows: 最简单的实现方法如下:

Copy the url of the remote repo by using GitLab/GitHub then make a new folder anywhere in your system, open gitbash and write the following command: 使用GitLab / GitHub复制远程仓库的URL,然后在系统中的任何位置创建一个新文件夹,打开gitbash并编写以下命令:

git init

git clone <Paste the url>

You will get all the files in the master in your folder. 您将在文件夹中获得主文件中的所有文件。

Then create a branch: 然后创建一个分支:

git branch <branch_name>

This will create aa new branch locally switch to the newly created branch as: 这将在本地创建一个新分支,切换为新创建的分支,如下所示:

git checkout <branch_name>

Now paste all the files that you wanted to push to remote in the git folder. 现在,将要推送到远程的所有文件粘贴到git文件夹中。

Now we just have to commit as follows: 现在我们只需要提交以下内容:

git add .

this command will add the files to the staging area. 此命令会将文件添加到暂存区域。

git commit -m"Any message"

to commit the changes. 提交更改。

git push origin <branch_name>

Done! 做完了!

where origin is the remote name Since we didn't create a new remote and cloned the existing remote repo. 其中origin是远程名称,因为我们没有创建新的远程并克隆了现有的远程存储库。 So by default the remote name will be origin 因此,默认情况下,远程名称将是原始名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM