0

I've pushed a file a.html from certain folder a (for example), but then, when I try to push another file index.html from another folder b in this way

git init
git add index.html
git remote add origin #link of my repository
git push -u origin master

I have got something like this

To https://github.com/NavasardianMichael/input.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '//link to my repository'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

I write the same commands both times, but the second time I have an error.

Joseph D.
  • 11,804
  • 3
  • 34
  • 67
  • What happens if you do ```git fetch``` ```git pull```? Did you add files directly to github? – joseluismurillorios Aug 01 '19 at 00:59
  • 1
    So you initialised a repo two? As in called `git init` in both folders? Then those folders will both be treated as separate Git repos. That's probably not what you want, but you'll need to make sure the second repo has the contents of the first repo (as in `pull`) before you can `push` from the second. – Obsidian Age Aug 01 '19 at 01:00
  • $ git pull There is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details. git pull If you wish to set tracking information for this branch you can do so with: git branch --set-upstream-to=/ master – Michael Navasardyan Aug 01 '19 at 01:04
  • Really I can't push another (not related to previously pushed files) file to my directory??) – Michael Navasardyan Aug 01 '19 at 01:10
  • If someone had this problem, please explain me and send right code, thanks to everyone) – Michael Navasardyan Aug 01 '19 at 01:17
  • Possible duplicate of [git: updates were rejected because the remote contains work that you do not have locally](https://stackoverflow.com/questions/24357108/git-updates-were-rejected-because-the-remote-contains-work-that-you-do-not-have) – phd Aug 01 '19 at 10:57
  • https://stackoverflow.com/search?q=%5Bgit%5D+hint%3A+Updates+were+rejected+because+the+remote+contains+work+that+you+do – phd Aug 01 '19 at 10:57

1 Answers1

0

No matter in a or b or any other repositories which are connected with the same remote repository, follow the procedure to pull and push.

# initialize the repo
git init
git remote add origin #link of my repository

# change, add and commit
git add <paths>
git commit -m'$(msg_generator)'

# update before push
git pull origin --rebase refs/heads/master:refs/heads/master

# push
git push origin refs/heads/master:refs/heads/master

It seems these commands are used in a script, so use --rebase instead of -r for readability, and use refs/heads/master:refs/heads/master instead of master:master and master to avoid hidden problems. For example, if there happens to be a ref refs/master which is created by mistake and unintentionally, git pull origin master would fetch refs/master instead of refs/heads/master.

For git commit, you need to define a function to generate commit messages automatically.

After git pull, there might be conflicts, which need handling.

ElpieKay
  • 27,194
  • 6
  • 32
  • 53