On Tue, 22 Feb 2022 at 07:58, Kuldeep Borkar
<kuldeepborkarjr...@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

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sympy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAHVvXxT_6G6YovtOf6rKXd3ir1GyYz2rCMrYKti87XXQ73AkTA%40mail.gmail.com.

Reply via email to