如何在新计算机上同步我的 git 存储库?

How to sync my git repository on a new computer?

我是 git 的新手。我在 bitbucket 上有一个存储库。现在想在上班的时候下载到自己的电脑上,对项目做一些修改,然后再push。

首先,我通过以下行在本地目录上初始化了 git:

$ git init

然后我设置一些配置:

$ git config --global user.name "my name"
$ git config --global user.email my.email@gmail.com

然后我就这样下载了:

$ git clone https://ShafizadehSajad@bitbucket.org/ ...

现在我可以在我的本地文件夹中看到该项目的文件。好的,很好!

我还通过以下命令在我的存储库和本地目录之间建立了连接:

$ git remote add origin https://ShafizadehSajad@bitbucket.org/ ...

现在当我输入 $ git remove -v 时,结果将是:

origin  https://ShafizadehSajad@bitbucket.org/ ... (fetch)
origin  https://ShafizadehSajad@bitbucket.org/ ... (push)

看起来不错!


当我输入此命令时:git status,我会看到所有 folders/files 都是红色的。因此,我输入了这个命令:

$ git add .

现在它们是绿色的!我还通过这一行为它写了一个提交:

$ git commit -m "there isn't any change"

然后为了将我的本地目录与 bitbucket 上的目录同步,我输入了这一行:

$ git push origin master

但是我得到这个错误:

$ git push origin master
To https://bitbucket.org/lamtakam/lamtakam.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://ShafizadehSajad@bitbucket.org/lamtakam/lamtakam.git'
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.

出了什么问题,我该如何解决?

一切正常,同时有人更新了存储库,在推送之前您需要集成您的更改:

git pull

似乎对远程存储库的更改可能需要您这边的一些 "merging"。如果是这样,您应该更改您的副本并提交这些更改,然后执行:

git push

如果在此期间没有人更改原始存储库,您应该能够推送您的更改。

hint: Updates were rejected because the tip of your current branch is behind

您需要先更新本地存储库(与远程同步)然后推送到远程 (origin)。

$ git pull origin master
$ git push -u origin master

你的第一步就错了:

git init

这会在您的当前目录中初始化一个全新的 git 存储库。你不想要那个,你只想克隆现有的存储库。

实际发生的是,您随后将现有存储库 克隆到您在步骤 1 中初始化的新存储库 中。嵌套 git 存储库有点复杂,但是最重要的是文件显示为红色,因为所有文件都是 新初始化的 git 存储库 的新文件(这与您在 bitbucket 中的存储库没有任何关系) .

您当前的目录树如下所示:

/htdocs
  /New Folder <--- you initialized the new git repo here
    /lamkatam <--- this is the git repo you cloned

跳过第一步重新开始,只执行 git clone(您可能想在 htdocs 文件夹中执行此操作)。您也可以跳过设置遥控器 - 当您进行克隆时,它会自动设置名为 origin 的遥控器并正确配置它。

git push (name of the dir)

例如:git 推送我的文件夹