【问题标题】:Add the file on the specific branch in git在git中的特定分支上添加文件
【发布时间】:2014-07-17 14:35:51
【问题描述】:

我是 git 新手,希望能正确使用它。

git init
git add .      // added the whole directory
git commit -m "initial stage"
git checkout -b "version1"
git branch

  master
* version1

现在我想在目录中添加一些新文件

cp /path/to/file/admin.php /path/to/git/folder/.

这些文件应该只包含在 version1 分支中,但它也包含在 master 分支中

git add admin.php
git status
On branch version1
Untracked files:
(use "git add <file>..." to include in what will be committed)
admin.php

git checkout master
git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
admin.php

问题是如何只在特定分支上添加文件?

谢谢

【问题讨论】:

  • 该文件在 either 分支上不存在;用 git 术语来说,它是“未跟踪的”。在您git add git commit 之前,它将继续不存在于存储库中的任何位置,仅存在于您的工作目录中。您需要从一些 git basics... 开始(*从技术上讲,git add 会将其保存为临时的,但在 git commit 步骤之前它不会成为永久性的。)
  • @torek 您的评论正是答案。那为什么不回答呢?
  • stackoverflow.com/a/2569513/3802077,@torek 所说的,但有更多信息和更具体
  • @RomanGotsiy:主要是因为完整的答案需要很长时间,并且与其他现有的 SO 答案和 Pro Git 书籍相比是多余的。这个问题暴露了对 git 的根本误解。

标签: git github


【解决方案1】:

要在特定分支中添加文件,请按照以下说明操作:

创建自定义分支

git branch branchname

切换到自定义分支

git checkout branchname

在自定义分支中初始化

git init

在自定义分支中添加文件

git add file name

提交在自定义分支中所做的更改

git commit -m "your message"

在您的 GitHub 存储库中进行更改

git push

希望你有明确的想法。

谢谢。

【讨论】:

    【解决方案2】:

    有时当你使用

    git add fileName
    

    你得到这个错误

    您的 .gitignore 文件之一会忽略以下路径: 文件名

    为了仍然添加它,请转到您的 .gitignore 并注释与该文件关联的行添加 #,

    #fileName
    

    然后您将能够添加、提交和推送。

    【讨论】:

      猜你喜欢
      • 2023-01-23
      • 1970-01-01
      • 2018-09-12
      • 1970-01-01
      • 2019-06-13
      • 1970-01-01
      • 1970-01-01
      • 2017-07-15
      • 2018-11-08
      相关资源
      最近更新 更多