Help with Git and GitHub

43 views
Skip to first unread message

Kuldeep Borkar

unread,
Feb 22, 2022, 2:58:27 AM2/22/22
to sympy
Hi SymPy Community,

Few days ago I ran into a problem regarding Git and GitHub;

Problem: Whenever I try to make a PR by creating a new branch from master branch, the past commits showed up in the commit history (which I think is the commits made from the master branch) .

Description: Every time I made a PR the commit history was like 12-14 commits and it was quite bad since even if I was making a few changes then also along with my recent 1 commit there were past 12 commits with it in the commit history.
I tried searching as much as possible what to do then I learned about rebase to edit the commit history and I tried that but I was able to see just the 1 recent commit there and nothing else so, I was confused a lot.
Now,
I think the problem probably is that I made some commits from the master branch itself and then even if I do something like fetch and merge, update the forked repository then also every time I try to create a new PR from a new branch created from master branch then also it is showing that past commits.
I realize this was my big mistake to make commits from master branch and I will avoid making any change in the master branch in future.

Is there any solution to this so that I can make PR and have the commits only related to the changes I made and not the past commits.

I asked on GitHub support there 1 solution I found was to delete the fork and refork the repository, 2nd solution I found was to create a branch from the upstream repository and merge branch to it but I am rookie at these things and was not able to understand that. I want to know if there are any other solutions to this.

Recently,
I created an issue related to the documentation in the stats module but again when I tried to make a PR there I got past 12 commits to see, therefore I used my another github account which I use on Linux to make a new PR where I just had 1 commit in commit history that's what I wanted to see.

(It's okay with me to refork the repository again after 2 PR I opened  gets merged because a few days ago I messed up everything while making a PR due to commit history I was trying to clean up and most people's commit showed up there which I feel bad about even today ; ( and this time I don't want to make any mess with the PR I made)

Please, provide some guidance.
Thank You!

Oscar Benjamin

unread,
Feb 22, 2022, 8:21:15 AM2/22/22
to sympy
On Tue, 22 Feb 2022 at 07:58, Kuldeep Borkar
<kuldeepbo...@gmail.com> wrote:
>
> Hi SymPy Community,
>
> Few days ago I ran into a problem regarding Git and GitHub;
>
> Problem: Whenever I try to make a PR by creating a new branch from master branch, the past commits showed up in the commit history (which I think is the commits made from the master branch) .
>
> Description: Every time I made a PR the commit history was like 12-14 commits and it was quite bad since even if I was making a few changes then also along with my recent 1 commit there were past 12 commits with it in the commit history.
> I tried searching as much as possible what to do then I learned about rebase to edit the commit history and I tried that but I was able to see just the 1 recent commit there and nothing else so, I was confused a lot.
> Now,
> I think the problem probably is that I made some commits from the master branch itself and then even if I do something like fetch and merge, update the forked repository then also every time I try to create a new PR from a new branch created from master branch then also it is showing that past commits.
> I realize this was my big mistake to make commits from master branch and I will avoid making any change in the master branch in future.
>
> Is there any solution to this so that I can make PR and have the commits only related to the changes I made and not the past commits.

Firstly as you now realise each PR should be made from a separate
branch and you shouldn't commit directly to your master branch. You
want the master branch in your fork to be only a pristine copy of the
master branch in the main sympy repo. To start make sure that you have
the main repo as the remote upstream:

git remote add upstream https://github.com/sympy/sympy.git

Now you update your master branch locally with:

git fetch upstream
git checkout master
git rebase upstream/master

You could use merge rather than rebase here. I use rebase because I
never want to create a merge commit but if you haven't committed to
your master branch then it shouldn't create a merge commit. If you do
accidentally commit to your master branch then always using rebase
here ensures that your accidental commits are always the last commits
on the branch.

Then when you want to create a PR you can update your master branch
(as above) and then make a new branch from it:

git checkout master
git checkout -b pr_branch

Of course you don't have to start your new branch from your local
master branch. You could instead do:

git fetch upstream
git checkout upstream/master
git checkout -b pr_branch

Then you have a local branch for the PR but it's created directly from
the upstream master branch so it won't have any of the extra commits
in it. This is the first solution to your problem if you just want to
create a PR right now.

If you want to get rid of the commits from your master branch then you
can do it like this but be careful because this is not reversible and
it is easy to mess this up. I'm assuming that you do not want to keep
any of the changes that you have made to your master branch and are
happy to delete them forever with no chance of recovery.

**Make sure that you have a backup of any valuable work and that your
working directory is clean before doing this.**

git checkout master
git reset --hard HEAD~1000 # Completely delete the last 1000 commits
git rebase upstream/master # Update from upstream again

I'm assuming that 1000 commits is enough to remove all the commits
that you have added but you can use a larger number if needed.

Now you've fixed your master branch locally but if you've been pushing
your master branch to your fork on GitHub then that also needs to be
fixed. A simple "git push" won't work now because you've "rewritten
the history". Instead a force-push is needed:

git push --force origin master

Both "reset --hard" and "push --force" are commands that should be
used sparingly. Make sure you understand what you are doing before
using them.

--
Oscar

Kuldeep Borkar

unread,
Feb 25, 2022, 5:08:21 AM2/25/22
to sympy
Thank you so much for the response,

After understanding things for a while, I tried to follow the steps and came across a problem:
  • After deleting the commits with
git reset --hard HEAD~1000 command

then I used

  • git rebase upstream/master  #till here it was all okay and I thought I just needed to force push the changes now, but when I did that it says
  • git push --force origin master
    Problem:  ! [remote rejected]       master -> master (refusing to allow an OAuth App to create or update workflow `.github/workflows/ci-sage.yml` without `workflow` scope)
    error: failed to push some refs to 'https://github.com/KuldeepBorkar/sympy.git'

    I was not able to find any perfect solution to this. 
    Therefore, I thought asking this before would be good or else I would run into another mess again😢

    Thank you for your help and guidance : )

    Qijia Liu

    unread,
    Feb 25, 2022, 9:11:45 AM2/25/22
    to sympy
    GitHub has restricted accessing repos using HTTPS. You have to set up SSH for your account, and modify .git/config under your repo to change https://github.com/KuldeepBorkar/sympy.git to g...@github.com:KuldeepBorkar/sympy

    Qijia Liu

    Kuldeep Borkar

    unread,
    Feb 26, 2022, 9:19:13 PM2/26/22
    to sympy
    Thank you so much finally it worked : )
    I used git remote set-url origin to set up SSH for the origin of repository.
    Reply all
    Reply to author
    Forward
    0 new messages