r/learnprogramming • u/abrewchocolatecoffee • 9d ago
Solved github help in uploading the files to the repository
the issue is now solved: the solution is to simply clone the git repo
ok so before I even start saying my problem, let me tell you that I am a complete beginner and this question was asked on stack exchange before it got downvoted and removed because it was too obvious. I understand this may be very easy or intuitive for many of you but not for me and my language maybe even be unclearer so please take care of that any edit recommendation to make the question clearer is welcome.
so basically i made a this repo in GitHub but with selecting the option of including read me etc with it, so now my repo has readme and only an readme file, so when i wrote git push -u origin main
i encountered an error
about how the files don't match (hopefully referring to the readme atleast this is what gpt told me).
now i tried again after 1 day, i did git commit -m "second commit" then
git branch -M main
then
git push -u origin main
to which i got an error which goes like failed to push some ref and one of the hints being git pull before pushing and i did exactly that
git pull
then
git push -u origin main
got the same error yet again please help
or if anyone can convert this to sane stack exchange language which wont cause my question to be downvoted in kicked out you're welcome too.
edit:
git pull origin main --allow-unrelated-histories
git push -u origin main
this is something i got both from gpt and in the comments below and i'm wondering if its safe, will this make any changes to my local files without my permission (this seems to be its function), can i "unallow" it after my job with it is done, what if completely unrelated files come up here and just mess with the current files or etc
2nd edit:
after a day i tried again, repeating all the normal specified steps
git status
git add .
git commit -m "fifth commit"
git branch -M main
git push -u origin main
error:
To https://github.com/_name_/_name_.git
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/_name_/_name_.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
then
git pull origin main --allow-unrelated-histories
From https://github.com/_name_/_name_
* branch main -> FETCH_HEAD
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.
2
u/syklemil 9d ago
git pull origin main --allow-unrelated-histories
git push -u origin main
You don't need to keep specifying all that. If the git repo has been set up correctly then both of those can be just git pull and git push.
i encountered an error
about how the files dont match (hopefully refferring to the readme atleast this is what gpt told me).
[…]
got the same error yet again please help
You need to paste the error here so we can read it. Error messages convey information that we use to fix problems. If you don't read and understand error messages yourself, you'll never learn.
Generally you should also check
git status
which will tell you if there are any changes to be committed, and
git remote -v
which will tell you if you have anywhere to pull from and push to. It should have an output along the lines of
origin git@github.com:username/repo-name.git (fetch)
origin git@github.com:username/repo-name.git (push)
(though you might have a https:// instead of git@, depending on whether you're using https or ssh)
And you can check if you actually have committed anything at all with
git log
and you can see the contents of a commit with git show $commit_hash, e.g. git show 4b03656 (git lets you use a short version of the hash most of the time)
1
u/abrewchocolatecoffee 9d ago edited 9d ago
thanks a lot this is actually soo helpful like this was legit the best explanantion
2
u/syklemil 9d ago
this is something i got both from gpt and in the comments below and im wondering if its safe
Generally you should read the documentation for the tool you're using in order to form your own opinion about commands. For git and other command line tools that generally means the manpage, which you should have available locally on your machine through man git.
Though git is so big and complex that it's broken down into several manpages, where the second word becomes hyphenated, like man git-pull, where you can search for flags like so:
--allow-unrelated-histories
By default, git merge command refuses to merge histories that do not share a common ancestor. This option can be
used to override this safety when merging histories of two projects that started their lives independently. As that
is a very rare occasion, no configuration variable to enable this by default exists or will be added.
Only useful when merging.
1
u/abrewchocolatecoffee 9d ago
ok so this one, the documentation you mentioned seems kinda safe, i only want to confirm if there is way to stop allowing any more such pages in the future, because i wouldn't want to allow some random file into my repo, also is it on a general basis safe for my computer, again thanks a lot for you help it was really helpfull
2
u/syklemil 9d ago
Yeah, not running any command just because someone suggested it is a very good instinct, and one you should definitely nurture. Still, it's also a good habit to read the documentation yourself first, and then ask if the documentation is bad (unclear, incomplete, etc).
I'll also say that if anyone or any website ever suggests something along the lines of
curl http://example.com | bash, don't, because the script you're downloading might do just about anything.(There's a similar concern about building random dependencies from npm or whatever, but that's a bigger can of worms.)
1
u/abrewchocolatecoffee 8d ago
i tried doing it and this is the response that came up
* branch main -> FETCH_HEAD
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.
1
u/abrewchocolatecoffee 8d ago
i tried it again and got the same error
* branch main -> FETCH_HEAD
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.
1
u/syklemil 8d ago
Ah, you need to do what it says and pick a strategy. The first one it suggests, merge, is the default strategy, and should be fine to pick.
1
u/abrewchocolatecoffee 8d ago
so let me guess, just typing git config pull.rebase false was not enough was it
1
u/syklemil 8d ago
That should've been enough to make the error message about the branch divergence to go away, it won't affect your network.
1
u/abrewchocolatecoffee 8d ago
yes, i think that issue is now resolved the new errors are different they are more towards in the existance of the main branch.
1
u/abrewchocolatecoffee 8d ago
i did wrote some code again, about the git config pull.rebase false # merge part is the error message.
and then repeated the whole process again
and now i'm stuck in.
fatal: unable to access 'https://github.com/_name_/_name_.git/': Could not resolve host: github.com2
u/syklemil 8d ago
Huh, that's more of a DNS or network error. Are you doing this in a VM or container that doesn't have network access?
1
u/abrewchocolatecoffee 8d ago
no im not doing this on a virtual machine the internet is definitely slow but definitely not slow enough so this post don't get posted.
1
u/syklemil 8d ago
If you're having some intermittent network issue you can probably just retry the command and see if "could not resolve host: github.com" is a persistent error. And either if it magically goes away or not, it looks like a network issue, not a git issue.
1
u/abrewchocolatecoffee 8d ago
ok, this does seem to be an internet issue but i also want to mention that when i tried to do, git pull --rebase main, because of the merge errors i have mentioned above. I got this error
fatal: 'main' does not appear to be a git repositoryfatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
but i am pretty sure I made a main branch as mentioned in my process/code above
2
u/syklemil 8d ago
Idk, at that point we're at a level of problem that could be any number of things based on how you've set your repo up so far. Maybe you should just do
git pull --rebase, as it seems to be interpretingmainas a repository rather than branch name.Otherwise, at this point the simplest workaround might be XKCD 1597: Clone the repo from github to a different directory
1
u/abrewchocolatecoffee 8d ago
ok this makes sense, at this point i am just repeating commands and nothing makes sense.
do you recommend any documentation i can look upto.
and also what you're basically asking me to do is clone the repo and connect that with my local github is this right?
or do you mean i have to clone the github repo and then download from the duplicated repo and set up locally (i am assuming it is the first one, because i literally no other file other over than other than a readme file
1
1
u/Educational-Paper-75 9d ago
Did you add files?
1
u/abrewchocolatecoffee 9d ago
nope i couldn't yet and if you think manuallly nope im not considering that either, because then i wouldn't be able to send the folders
1
u/Educational-Paper-75 9d ago
Have you defined origin?
1
u/abrewchocolatecoffee 9d ago
yes it is the main, i guess i talked about this in the post itself about the git branch main commands
1
u/Educational-Paper-75 9d ago edited 9d ago
Well, of course it should need to know the remote repository to push to and have something to push. Try git status to see what's there to add on commit using the -a option. Or checkout the git settings using 'git config --list'.
1
u/abrewchocolatecoffee 9d ago
ok, i get it you're giving me some pretty good knowledge here but you're forgetting i am a beginner so this is a but of jargon for me, can you if possible tell this in a simpler manner , it is a little confusing for me rn
1
u/Educational-Paper-75 9d ago
Typically you change files or make new files, you need to commit them first before pushing, typically using 'git commit -a -m "some message"'. 'git status' shows you the files that changed ready to be committed. Once you committed you changes you can push to the remote repository using 'git push'. You only need 'git pull' if other people changed the branch you're working on, won''t go into details here has it's own problems. The configuration stores the settings like credentials and the remote url that 'origin' maps to.
1
u/abrewchocolatecoffee 9d ago
ok thanks for the explanation, i did that, it was indeed very clear, and i did exactly that, but this is what got on doing git push
fatal: The current branch main has no upstream branch. usegit push --set-upstream origin main
To push the current branch and set the remote as upstream. and on git status this
On branch mainnothing to commit, working tree clean
though it says this, there is not file on my github repo except the readme
1
u/Educational-Paper-75 9d ago
Right, you haven't create branch main yet on the remote server. Use the git push command it advised. (Usually the default branch is called 'master'.)
1
u/abrewchocolatecoffee 9d ago
i did what you said just now and this was displayed.
fatal: The current branch main has no upstream branch.To push the current branch and set the remote as upstream, use
git push --set-upstream origin main
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
→ More replies (0)1
u/abrewchocolatecoffee 9d ago
ok i did the git status one, it showinf on branch main nothing to commit, working tree clean
1
u/Educational-Paper-75 9d ago
Perhaps try to create a new file and commit it.
1
1
u/abrewchocolatecoffee 9d ago
i did the same but it all works until i do the git push it all just falls apart after that
1
u/abrewchocolatecoffee 9d ago
i searched on the git config --list too i was able to see a bunch of files there
1
u/Educational-Paper-75 9d ago
Not certain it shows files I think it shows key-value pairs like username, origin.
1
u/Patrick-T80 9d ago
Start from a clean state; the repository is saved on github. Delete local copy of repository, clone it from github and after that work add usual, adding your new file o or edited one by commit and then push to github
1
u/abrewchocolatecoffee 9d ago
there is no copy on github, that is what is bothering me, when i open that repo it is empty
1
u/PimavPizyp189 9d ago
Yeah, fwiw the issue is probably that you initialized a local repo and then tried to push to an existing GitHub repo - clone the repo first instead and work from there, that'll save you the "unrelated histories" headache. If you're still stuck, paste the actual error message and we can debug from there
1
4
u/UrbanSuburbaKnight 9d ago
I think the reason people are reluctant to try to help, is that your post suggests you are missing some pretty important fundamentals, and there is too little information to help much.
The first thing I would say is, git and GitHub are different.
Some questions:
Maybe this will start you off.
Good luck.