git 在git中的特定分支上添加文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24806469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 18:18:33  来源:igfitidea点击:

Add the file on the specific branch in git

gitgithub

提问by prabhu

I am new to git and hope using it properly.

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

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

  master
* version1

Now i want to add some new files in the directory

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

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

These files should include only on version1 branch but it is also including in the master branch

这些文件应该只包含在 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

The question is how can i add files only on specific branch??

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

Thanks

谢谢

回答by nawazlj

In order to add files in specific branch follow the below instructions :

为了在特定分支中添加文件,请遵循以下说明:

To create custom branch

创建自定义分支

git branch branchname

To switch into custom branch

切换到自定义分支

git checkout branchname

To initialize in the custom branch

在自定义分支中初始化

git init

To add files in the custom branch

在自定义分支中添加文件

git add file name

To commit the changes made in the custom branch

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

git commit -m "your message"

To make changes in your GitHub repo

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

git push

Hope you get clear cut idea.

希望你有明确的想法。

Thank you.

谢谢你。

回答by u5675325

Sometimes when one uses

有时当一个人使用

git add fileName

one gets this error

一个得到这个错误

The following paths are ignored by one of your .gitignore files: fileName

您的 .gitignore 文件之一将忽略以下路径:fileName

In order to still add it, go to your .gitignore and comment the line associated with that file adding #,

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

#fileName

Then one will be able to add, commit and push.

然后就可以添加、提交和推送。