【问题标题】:Commit gitignore first time after creating a new repo in github在 github 中创建新仓库后第一次提交 gitignore
【发布时间】:2020-03-05 05:35:02
【问题描述】:

我在 Github 中创建了一个新的仓库,现在我只想推送 gitignore 文件。在下一次提交时,我想推送其余文件。

我做的步骤--

> git branch
* master
> git branch -r
> git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   .idea/inspectionProfiles/Project_Default.xml
    new file:   .idea/vcs.xml

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   .gitignore
    modified:   .idea/workspace.xml

所以我把 .idea/workspace.xml.idea 文件夹一起放在了 gitignore 中

> git add .gitignore
> git commit -m 'gitignore only'

我的问题是什么是推送命令,因为远程仓库中没有分支。

非常感谢任何帮助。

更新-1

我尝试了以下 -

git add .gitignore
git commit -m "git ignore only"
git push origin master

现在,当我检查远程 github 时,我可以看到所有文件都已添加。

【问题讨论】:

  • @Yousaf 不,我先创建了本地存储库,然后创建了远程存储库。现在我想第一次只提交 gitignore 文件。没有其他的。上次我尝试它提交了所有文件。
  • @CodeCaster 我没有推出任何新的分支。我想要的是将 gitignore 文件仅推送到远程。我认为这与您可能的重复链接没有任何关系。我刚刚添加了 gitignore 文件并提交,在 github 上我可以看到所有文件以及 .idea 文件夹。
  • 那么你的 github 仓库有一个分支。
  • 不..我刚刚展示了 git branch -r 结果。

标签: git github


【解决方案1】:

Project_Default.xmlvcs.xml 这两个文件已经处于“暂存”状态。将它们添加到.gitignore 不会影响在下一个git commit 中,它们将被集成到提交中(因为它们是“暂存的”)。

正如我们在gitignore documentation 中看到的那样:“Git 已经跟踪的文件不受影响”

【讨论】:

  • 谢谢。这是有道理的。
  • 不客气,你能支持我的解决方案(甚至接受它)吗?
【解决方案2】:

所以,回顾一下:

  • 您已经在本地创建了您的 repo 并对其进行了初始化 (?)
  • 您位于名为 master 的本地分支上。
  • 没有远程分支
  • 您已上演(git add).idea/inspectionProfiles/Project_Default.xml.idea/vcs.xml
  • 你也上演了.gitignore
  • 您只想推送.gitignore

我理解正确吗?那么你需要:

  1. 取消暂存 .idea/inspectionProfiles/Project_Default.xml.idea/vcs.xml 使用
$ git reset .idea/inspectionProfiles/Project_Default.xml
$ git reset .idea/vcs.xml
  1. 将当前本地 master 推送到远程 repo(假设它被称为“origin”并在那里创建一个“master”分支):
$ git push origin master
  1. 设置本地主机跟踪远程主机:
$ git branch --set-upstream-to=origin/master master

作为旁注,您可以通过指定远程分支名称来执行第 2 步:

$ git push origin master:remotemaster

【讨论】:

  • 实际上在第一次提交到远程仓库时(如果没有可用的远程分支),它将推送所有文件而不是单个文件。不知道这是对还是错?
  • 是否有远程分支无关紧要。它只会推动你所承诺的。所以任何没有通过“git add”然后“git commit”的东西都只会留在本地分支上
猜你喜欢
  • 2015-12-24
  • 2022-06-24
  • 2021-03-24
  • 1970-01-01
  • 1970-01-01
  • 2018-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多