Sep 14, 2024

Editing a Pull Request from a fork using git cli

git

There are some cases where you want to edit the PR which is made from a fork of your repo. Let's see how we can do this from git CLI itself.

Let's say you've a project on github and someone made a from and created a PR, but you wanted to add something before merging it with yours, but he is not replying and you really like the PR. - then you decided to edit that PR by yourself and merge. But HOW?

#. Break it down

someone named danish created a fork and worked on a branch called feat/smthn and created a PR.

First thing you should check is- if that PR is editable by maintainers (check very bottom right section of PR).
If it's checked, then you're good to go (it is by default).

Allowing edit from maintainers GH docs

#. Action time

First we need to create temporary remote to forked repo.

bash
git remote add temp-danish-remote git@github.com:danish/your-super-repo.git

This will add a remote to your fork (you can remove it later).
To verify run this command:

bash
git remote -v # would show something like: # temp-danish-remote git@github.com:daaniissh/your-super-repo.git (fetch) # temp-danish-remote git@github.com:daaniissh/your-super-repo.git (push) # origin git@github.com:moonlitgrace/your-super-repo.git (fetch) # origin git@github.com:moonlitgrace/your-super-repo.git (push)

That's done, now let's update newly added remote.

bash
git fetch temp-danish-remote

This will fetch every changes made in fork to remote.
Then we need to create a branch for us to work on which is connected to remote branch which danish worked on.

bash
git checkout -b danish-feat-smthn temp-danish-remote/feat/smthn

This will create and checkout to branch danish-feat-smthn which has changes made by danish.

Make some changes...

After making some changes, do git add . and commit it with a message.
Now- we can't directly push to branch danish-feat-smthn, we need to push to remote branch called feat/smthn which is in remote temp-danish-remote.
To do so:

bash
git push temp-danish-remote temp-danish-remote:feat/smthn

You could alternatively use HEAD:feat/smthn instead temp-danish-remote:feat/smthn

Now check that PR- your commit will be there!

#. Potential issues

If you see something like ! [remote rejected] HEAD -> main (permission denied).
The other user probably disabled edit from maintainers option. Have them checked and try again.

#. Helpful links