More git blues - only want to upload changes from local drive to GitHub

I managed to sync up local directory with remote project (i.e. on GitHub).

But now I made some changes in local working directory and want to push those to remote location.

These are the steps I took:

  1. git status to see what was different from last commit.
  2. As expected, it named a couple of files that I had changed and 1 brand new file that i had just created.
  3. I added all of those files using git add <filename>
    Side question: is there a way to add all files that get identified by git status so I can execute git add just once and capture all those files?
  4. Then I tried to push with: git push origin master and got the following error:
  5. So after some googling (and mostly getting confused), I tried to pull: git pull
  6. Then I just went down a rabbit hole that is not worth listing out in full…

Bottom line: Can someone please help me understand what I should have done in order to: upload changes made on laptop with GitHub…? Was I supposed to have created a branch first…?

First I would change your topic’s category to git.

  1. You will need to specify which repository you to pull it from. If you have already pushed some changes from local to remote then it should be fairly simple. Use git pull origin master.

Side question: it’s git add .

After adding (you can use gitt add . to add everything), did you do a git commit?

git commit -m “fix bug”

before doing a

git push

I just wanted to chirp in with a “for future reference” comment.
git help will give you a pretty good description of the git commands in your terminal. You can get even more information about a specific command you’re trying to understand by using git help <command>.

For example, typing git help pull will open the relevant manual pages in your browser. Official documentation (“man pages”) can take some experience to be super helpful, but I do reccommend that as part of your googling process you read the man pages as well as the more helpful explanations to get you used to the language of the documentation.

Type:

git remote -v

If you get nothing. Add origin:

git remote add origin https://github.com/SabahatPK/vidly.git

Then type the last line you got when you did pull:

git branch --set-upstream-to=origin/master master

No you should have added remotes. Check with:

git remote -v

You should get two lines in response.
Now:

git pull

Then add, commit and push changes:

git add .
git commit -m "Commit message"
git push
1 Like

Thanks @jenovs. But when I get to the git pull step I get a message saying “fatal: refusing to merge unrelated histories” After googling, it looks like this is a message for preventing (as message suggests) completely unrelated pieces of code from being merged together. It can, however, be forced which at this point, I’m willing to try: git pull origin master --allow-unrelated-histories.

This led to another error: Your local changes to the following files would be overwritten by merge.

This might be because I have done so many things wrong (run commands out of order) by this point that that is a valid error.

However, what is the best way to re-start this process? Delete remote repo and follow steps from scratch to update remote repo (as per my steps here)?

Thanks.

For small, private repos usually simplest way is to just start from scratch (i.e. delete either local or remote repo leaving one with the newest code).

You could try force update remote repo (you should never do it in real life, unless you really know what you’re doing):

git push --force

Relevant: https://xkcd.com/1597/

1 Like

So…it turns out I did something VERY stupid here. In Command Prompt, I was in the folder just ABOVE my project folder. I was in Start when I should have been in Start/vidly.

Lesson. Learned. The really hard way. Always check the folder you are in!!

Foolproof way to ensure you are always in the right directory: open project in VS Code, right click on top folder of project you are working on, select Reveal in Explorer, then in Windows Explorer, right click the folder and select Git Bash Here. It sounds convoluted but it helps me make sure I am always looking at the right folder. It doesn’t feel too onerous since VS Code is usually open anyway.

Hope my frustration saves others a few minutes/hours.

I’m so glad you figured it out. Being in the wrong directory is definitely a mistake we all make forever. :smiley:
If you’re using VS Code, I believe that they actually have a decent Git integration built in! (Getting used to doing it via the command prompt is never a bad idea though.)

1 Like

Actually since VS Code got git integration I haven’t added/commited from command line once. Just select needed lines, “Stage selected ranges” and add commit message. Nice atomic commits.

Pushing/pulling/branching etc. still from command line although it also could be done from VS Code.

1 Like