How does a force push affect a cloned branch? - github

I was wondering if a branch is cloned from master. And now you remove some commits from master and force push. Will those commits be deleted on the cloned branch as well?

No: a branch starting from a commit of another branch will still reference that commit, even if that other branch changes (through a forced push) its HEAD.
You might want to rebase your branch on top of the new reset master, if you don't want to continue referencing those old commits.
git fetch
# see that origin/master has been reset
git checkout mybranch
git rebase --onto origin/master master myBranch
In that scenario, master is still locally at the old HEAD (from which you current branch is starting).
origin/master has been reset (and is up-to-date after the git fetch)
After that, you can reset your own local master:
git checkout master
git reset --hard origin/master
(make sure you don't have any work in progress when doing a git reset --hard)

Related

Accidentally "Merged remote-tracking branch 'origin/x' into x", how can I remove this completely from my repository?

I accidentally made the same commit twice, and then somehow merged them and now there's this commit called "Merge remote-tracking branch 'origin/lab1' into lab1" Also my local repository is apparently not synced, this seems simple but I'm stuck
Tried squashing commits into one on github desktop, but it says "Unable to squash. Squashing replays all commits up to the last one required for the squash. A merge commit cannot exist among those commits."
I know how to fix this on my local repository, but how do I remove that merge commit from github?
If that merge commit is the latest one on lab1, you can simply reset one commit earlier (make sure you don't have any work in progress):
git switch lab1
git reset --hard lab1~
git fetch
git rebase origin/lab1
git push
But you have done commits after that merge commit, then you need mark them and replay them:
git switch lab1
git branch <sha1 of the merge commit> m
git branch lab1 tmp
git reset --hard m~
git fetch
git rebase origin/lab1
git rebase --onto lab1 m tmp
git switch lab1
git merge tmp
git push

Is it possible to delete specific commits from forked repo by github website

I searched this but unable to find my answer.
And I just want to know the non command line way to do this....
Or is there any other way to achieve this same ?
You can delete files (from a repo you own, like a fork)
But you cannot directly from the web interface delete commits.
For that you would need to clone, do an interactive rebase (dropping the commits you don't want) and git push --force.
Even a revert does not seem possible.
The OP adds:
Actually I have 3 branches one is master and rest two are patch-1 and patch-2 and I messed up with master branch before creating patch-1 and patch-2 branches so some useless commits are also cloned from master to patch-1 and patch-2 branch so please can you help me to delete those commits which are cloned from master?
No without cloning the repo though:
git clone /url/of/your/fork
cd yourfork
git remote add upstream /url/original/repo
git fetch upstream
git checkout -b patch-1 origin/patch-1
git checkout -b patch-2 origin/patch-2
Then replay your patches on top of upstream master:
git rebase --onto upstream/master master patch-1
git rebase --onto upstream/master master patch-2
Finally reset master to the upstream master branch:
git checkout master
git reset --hard upstream/master
And then push everything back to your fork:
git push --force --mirror

GitHub feature branch

I have forked a repo with master branch . I have few changes here committed. I have to work a some feature now.how can I create a feature branch with latest from upstream and without the commits made to my master branch in my fork
Very simple actually.
Create the new branch feature, fetch upstream/master and reset feature:
# fetch latest `master` from `upstream` remote
$ git fetch upstream master
# create and checkout new `feature` branch off your current branch (`master`)
$ git checkout -b feature
# reset `feature` branch to `upstream/master`
$ git reset upstream/master --hard

How to merge master branch in local feature branch

In one of case I have created an branch and started to work on. I keep on commit & push changes in local branch but did not merge in master & neither pulled any changes from master.
Now I'm done with local branch changes. I followed derekgourlay tutorial & followed following steps to merge my project.
git fetch origin
git rebase −p origin/develop
First it game me number of conflict which was obvious but changes that I committed in my local branch those are not there after merge.
Am I missing anything. Any suggestion?
You can merge develop branch with your feature branch.
$ git checkout feature
$ git pull origin develop # pull (fetch + merge) develop branch into feature
$ git push origin HEAD # update remote/feature

Git Rebasing with multiple committers on same branch

We ran into a problem where, if you rebase your branch on top of master and then push, the other guy who was working on your branch pulls and gets all kind of merge conflicts, because the rebase has rewritten the history and the commits are not the same as they were on the branch. Is there any way around this, assuming that the other guy has some commits he wants to push to the branch?
Others should be able to rebase the branch after your push by using
git pull --rebase
You can check the "Recovering From Upstream Rebase" section of the git rebase man page.
Basically, your colleague will have to do (considering here 'subsystem' as having been rebased):
git rebase subsystem
(if his changes are on a separate branch)
or:
git rebase --onto subsystem subsystem#{1}