Git 添加文件夹并推送

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24945657/
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-19 10:14:27  来源:igfitidea点击:

Git add folder and push

gitatlassian-sourcetree

提问by Jalle

Currently I have those files in remote repository:

目前我在远程存储库中有这些文件:

README 
run.bat 
MyCalories2 <-FOLDER THAT IS EMPTY BUT NEED TO HAVE PROJECT FILES

The problem is that MyCalories2 is a folder and none of the files are saved in repo. I tried with git add .and also tried adding this folder from SourceTree but they are simply never uploaded to the remote repo.

问题是 MyCalories2 是一个文件夹,没有任何文件保存在 repo 中。我尝试git add .并尝试从 SourceTree 添加此文件夹,但它们从未上传到远程存储库。

$ git add .
$ git push -u origin master
Branch master set up to track remote branch master from origin.
Everything up-to-date

but its not ! How do I add the complete folder to git ?

但它不是!如何将完整文件夹添加到 git ?

 ~/Dropbox/AndroidStudioProjects/test/xxx (master|MERGING)
$ git push -u origin master
Username for 'http://git.yyy.com': xxx
Password for 'http://yyy.com':
To http://git.yyy.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'http://yyy.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.

This is latest error msg. real URLs are masked with 'yyy'

这是最新的错误消息。真实的 URL 被 'yyy' 屏蔽

@Marcus I tried all of these commands

@Marcus 我尝试了所有这些命令

ls -la . MyCalories2

MyCalories2:
total 15
drwxr-xr-x    1 Jalle    Administ     4096 Jul 25 21:46 .
drwxr-xr-x    1 Jalle    Administ     4096 Jul 25 21:49 ..
-rw-r--r--    1 Jalle    Administ       64 Jul 16 19:59 .gitignore
drwxr-xr-x    3 Jalle    Administ        0 Jul 25 14:37 .gradle
drwxr-xr-x   15 Jalle    Administ     4096 Jul 25 21:51 .idea
-rw-r--r--    1 Jalle    Administ      870 Jul 16 20:01 MyCalories2.iml
drwxr-xr-x    1 Jalle    Administ     4096 Jul 25 21:24 app
drwxr-xr-x    3 Jalle    Administ        0 Jul 25 14:37 build
-rw-r--r--    1 Jalle    Administ      452 Jul 24 21:33 build.gradle
drwxr-xr-x    3 Jalle    Administ        0 Jul 25 14:37 gradle
-rw-r--r--    1 Jalle    Administ      870 Jul 16 19:59 gradle.properties
-rwxr-xr-x    1 Jalle    Administ     5080 Jul 16 19:59 gradlew
-rwxr-xr-x    1 Jalle    Administ     2404 Jul 16 19:59 gradlew.bat
-rw-r--r--    1 Jalle    Administ      435 Jul 16 19:59 local.properties
drwxr-xr-x    5 Jalle    Administ        0 Jul 25 14:37 parseproj
-rw-r--r--    1 Jalle    Administ       16 Jul 16 19:59 settings.gradle


$ git --version
git version 1.9.4.msysgit.0

$ git pull
U       MyCalories2
M       run.bat
Pull is not possible because you have unmerged files.
Please, fix them up in the work tree, and then use 'git add/rm <file>'
as appropriate to mark resolution, or use 'git commit -a'.

$ git mergetool
Merging:
MyCalories2

Submodule merge conflict for 'MyCalories2':
  {local}: deleted
  {remote}: submodule commit 4bf1b974662b5b694f985ee93401227262fd3f26
Use (l)ocal or (r)emote, or (a)bort? l


$ git push -u origin master
Username for 'http://yyy.com': yyy
Password for 'http://yyy.com':
Counting objects: 245, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (215/215), done.
efrror: RPC failed; result=22, HTTP code = 4136.00 KiB/s
Watal: The remote end hung up unexpectedly
Writing objects: 100% (243/243), 1.29 MiB | 136.00 KiB/s, done.
Total 243 (delta 99), reused 0 (delta 0)
fatal: The remote end hung up unexpectedly
Everything up-to-date

But why this error ?!?!

但是为什么会出现这个错误?!?!

Any help, please ? I am stuck here !

有什么帮助吗?我被困在这里!

and finally here is my config file

最后这是我的配置文件

[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
    hideDotFiles = dotGitOnly
[remote "origin"]
    url = http://git.yyy.com/My-Name/My-Name.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[http]
    postBuffer = 524288000
[remote "ssh"]
    url = [email protected]:My-Name/My-Name.git
    fetch = +refs/heads/*:refs/remotes/ssh/*
    pushurl = [email protected]:My-Name/My-Name.git

回答by Marcus

Ok, now the issue at hand is a completely different one than originally...

好的,现在手头的问题与最初的完全不同......

From the output of git mergetoolit's apparent that MyCalories2:

从输出git mergetool可以看出 MyCalories2:

  1. exists in the remote repository.
  2. is actually a git submodulein the remote repository.
  1. 存在于远程存储库中。
  2. 实际上是远程存储库中的 git子模块

Most probably you should notjust replace that submodule with your in-repository copy of MyCalories2.

最可能的是,你应该只是替换MyCalories2您在存储库副本子模块。

What you need to do is git clonea fresh copy of the repository, then copy the changes you want to commit (if any) from your old copy to the new copy. Then please see this answerfor info about how to push changes in a submodule.

您需要做的是git clone存储库的新副本,然后将您要提交的更改(如果有)从旧副本复制到新副本。然后请参阅此答案以获取有关如何在子模块中推送更改的信息。

回答by Raja Simon

Do with initially like

做与最初喜欢

git init

and then if you are reading any file or folder then do with this command

然后如果您正在阅读任何文件或文件夹,请使用此命令

git add <file/folder name>

or

或者

if you are adding lot of files then git add .also enough to add all files at once

如果您要添加大量文件,则git add .也足以一次添加所有文件

then commit the files

然后提交文件

git commit -m "First commit"

then push to the branch like

然后推到分支像

git push 

And the error message saying that the file up to date with repo so pull and rebase gonna work

并且错误消息说文件与 repo 是最新的,因此 pull 和 rebase 会起作用

git pull 

git rebase