0

I had asked another question but realized I miss-stated so deleted it. How do you resolve the following situation with Git? You have a repo. You clone the repo and make some changes to the source code. Before pushing, you use Github.com to add a new file (for example you create a readme file). I think you have a merge conflict if you try to push, but it seems trivial to fix since the files themselves don't contain conflicts. How do you resolve this?

I've had trouble in the past where I started working locally and went to make a repo and initialized it with a readme file (that the local copy didn't have) and got errors.

TL;DR I'm asking what happens if there are different files in the local repo (or the repo hasn't been initialized) and the remote repo, but the individual files don't conflict.

Celeritas
  • 14,489
  • 36
  • 113
  • 194
  • `git stash && git pull --rebase && git stash pop`, read about the stash: https://git-scm.com/docs/git-stash –  Jun 04 '16 at 07:08

2 Answers2

1

If the individual files don't conflict and you try to push from the local repository to the remote repository on the same branch, there will not be a merge conflict.

Most likely, you will be prompted to git pull before pushing your changes. Git will recognize they do not have conflicting information, and the additions on the local repository will be added to the remote repository when you run git push.

There's no need to rebase or stash anything.

Briana Swift
  • 1,047
  • 8
  • 9
  • I find `git` very confusing. It didn't add the new file when I ran `git pull`, however the command completed without errors. When I added the repository, `git pull http://...path.to.my.repo` it worked. Do you know why? – Celeritas Jun 04 '16 at 23:40
  • What is the output when you run `git remote`? – Briana Swift Jun 04 '16 at 23:48
  • It's `origin`, now. – Celeritas Jun 05 '16 at 02:13
  • Ah I see - that was my mistake before. When you run `git remote -v`, does it output the link to the remote repository? – Briana Swift Jun 05 '16 at 03:37
  • Yes it does, but I've restarted my comp since the problem so idk. – Celeritas Jun 05 '16 at 04:39
  • I guess what I'm really wondering is when can you just do `git pull` and when do you need to specify the remote repository? – Celeritas Jun 05 '16 at 04:40
  • If the local and remote are linked correctly (check if they are with `git remote --v`), then all you should need to do is `git pull` and the branch that you're on should be updated. – Briana Swift Jun 05 '16 at 14:37
  • Ok thanks. What situations cause the local and remote not to be linked correctly? – Celeritas Jun 05 '16 at 20:34
  • That wouldn't happen unless someone made it that way through their own local machine. It wouldn't affect your initial question of how Git recognizes different files on the remote and on the local repository. – Briana Swift Jun 06 '16 at 03:43
0

Even better than git stash, I always prefer adding and committing locally first.

Then a git pull --rebase will replay that local commit on top of GitHub content.

Don't forget that since git 2.6 (Sept. 2015), you can set that mode by default with stashing (if you forget to commit first):

git config --global pull.rebase true
git config --global rebase.autoStash true

See more at "Can “git pull” automatically stash and pop pending changes?".

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250