Saha
Saha

Reputation: 89

git unable to merge my local branch with remote

I cloned a git repo of my org like below

git clone https://giturl/my_org_name/runbooks

It asked for my username/password and successfully cloned it.

And then I checked out a new local branch

git checkout -b patchv1

I made a couple of commits.

then I checkout to master and merged my patchv1 branch with local master.

git checkout master

git pull

git merge patchv1

Below is my git status output

git status

On branch master
Your branch is ahead of 'origin/master' by 4 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

git remote show origin

* remote origin   
Fetch URL: https://giturl/my_org_name/runbooks.git   
Push  URL: https://giturl/my_org_name/runbooks.git   
HEAD branch: master 
Remote branches:
   upgrade-schedules           tracked
   ...................           .....
   patchv2                     tracked   
Local branch configured for 'git pull':
  master merges with remote master   
Local ref configured for 'git push':
  master pushes to master (fast-forwardable)

I want to push my local master branch to remote origin, but with a different name. So, I use below command with -u so that the remote branch gets created

git push -u origin master:newpatchv2

Got below error

remote: Permission to my_org_name/runbooks.git denied to my_username. fatal:
unable to access 'https://giturl/my_org_name/runbooks.git/': The
requested URL returned error: 403

From the webUI, I tried to edit a file in master branch of remote repo and got below error,

"You’re editing a file in a project you don’t have write access to. 
Submitting a change to this file will write it to a new branch in your fork **my_username/runbooks**, so you can send a pull request."

I read further about this and found the below statement

Changes made to the forked repository can be merged with the original repository via a pull request. Pull request knocks the repository owner and tell that “I have made some changes, please merge these changes to your repository if you like it”. On the other hand, changes made on the local machine (cloned repository) can be pushed to the upstream repository directly. For this, the user must have the write access for the repository otherwise this is not possible. If the user does not have the write access, the only way to go is through the forked request. So in that case, the changes made in the cloned repository are first pushed to the forked repository and then a pull request is created.

So, I went to my org's github webui repo page (url - https://giturl/my_org_name/runbooks) and clicked on fork. It said,

"You've already forked runbooks" and showed me the below repo

my_username/runbooks

Now, as per the statement what I read, I can push my changes to my forked repo my_username/runbooks and then create a pull request. Right? But in my org's github webui runbooks repo page, If I click on "New Pull Request"

Under "Compare Changes", I see like below

base: master   <---  compare: <drop_down>

when I click on the drop_down I don't find my_username/runbooks repo at all.

Two questions:

  1. How to push my local master branch to my forked repo - my_username/runbooks ? Should I need to change my remote origin somehow since I cannot create a new remote branch in my org's repo?

    The url to my forked repo is like below

    https://giturl/my_username/runbooks?organization=my_username&organization=my_username

  2. Once I push my local master to my forked repo - my_username/runbooks, how to create a pull request in my my_org_name/runbooks repo as my_username/runbooks is not being listed either in the base or in the compare of "Compare Changes" page.

Sorry if the question is very basic. But I have a hard time understanding this and appreciate any help.

Upvotes: 1

Views: 1114

Answers (1)

erik258
erik258

Reputation: 16275

Pro tip: never change the master branch on a fork of a repo, or any branch that came from upstream. Your master branch will diverge from upstream and you won't be able to pull upstream into the local branch.

In your workflow, what would have made more sense is before making your commits, create a new local branch, git checkout -b newpatchv2, and commit to that. Push newpatchv2 to your fork, and then create a PR in the upstream repo to request they pull in your changes. They may not accept the PR as is, and if your changes are already in your master branch, you won't be able to sync back up with the remote master, having diverged history by pushing changes into your local master that aren't (yet) in upstream and may not ever be (pending PR approval). Remember, git is a distributed change control system, and other contributors' PRs may get into upstream master before your own.

If you do end up diverging your local master branch, the easiest fix is to branch off your local master to a new branch (say old-master), then delete your local master branch and pull remote master back down. Once you're in sync with master, keep it that way - branch offf master to make changes, then pull those changes back into master only when they've been merged to upstream master.

Now let's get to your questions.

How to push my local master branch to my forked repo - my_username/runbooks ? Should I need to change my remote origin somehow since I cannot create a new remote branch in my org's repo?

Since git is a distributed version control system, having multiple remotes is perfectly fine. In this case, you might even have 3 remotes - your organization's remote, your personal fork, and the upstream remote from which the org forked. The only special thing about the origin upstream is that if you clone a repo, it automatically creates a remote source in the local repo, and calls it origin.
You can add more origins:

git remote add myfork https://github.com/.../..

Then you can git push myfork instead of git push origin. Note there I'm not specifying a branch. Keep things simple! Until you consider yourself a git adept, branch before committing and keep the remote branch the same as your local branch.

Once I push my local master to my forked repo - my_username/runbooks, how to create a pull request in my my_org_name/runbooks repo as my_username/runbooks is not being listed either in the base or in the compare of "Compare Changes" page.

There is a "compare across forks" button that should allow you to select a different fork for the destination of your PR. See this doc from github for screenshots of what that looks like:

https://docs.github.com/en/free-pro-team@latest/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request-from-a-fork

Upvotes: 2

Related Questions