Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: What happens to a branch after a master reset

Tags:

git

git-reset

Assume I have a master branch with some commits I want to keep for later documentation and reference but remove them from the master (because I want the master to be on the same state as the upstream repo).

My approach now is, to

  1. Create a new branch from current master state
  2. Reset (--hard) the master to a state which is present in the upstream repo as well

Now, my question is:

  • What happens to the new branch when removing the reference commit from the master?
  • Or am I completely wrong in the understanding of how branches are referenced.

Often branches are shown in the following way, where (to my understanding) D is the basis of the new branch.

A - B - C - D    (master)
             \
                 (new branch)

Is the branch "rebased" automatically or how would you call it? Would it then look like this?

A - B         (master)
     \
      C - D   (new branch)

Last and maybe most general question:

  • Is my approach of keeping the state in a new branch and reset --hard the master branch the right way to achieve my goal of getting my fork back to the upstream state (commit B) without having my commits (C and D) merged?
like image 842
freiheitsnetz Avatar asked Mar 07 '19 18:03

freiheitsnetz


People also ask

What happens when you reset a local git branch to remote?

When resetting a local Git branch to remote, you will lose the changes you made locally. This step is optional, and you may choose to do it just in case something goes wrong or you want to go back to that work again in the future. Your work now is saved to the branch named backup_work.

How do I REBASE changes in Git?

In Git, this is called rebasing . With the rebase command, you can take all the changes that were committed on one branch and replay them on a different branch. For this example, you would check out the experiment branch, and then rebase it onto the master branch as follows: $ git checkout experiment $ git rebase master First, ...

How does Git reset hard work?

This isn't a matter of updating the working tree and checking out a certain version of the content of tracked files. The git reset hard command actually points the HEAD right back to the old commit and any changes to tracked files in the working tree since then are discarded.

How do I list all of my local Git branches?

And to list all of your local branches, you use the git branch command. To collaborate with other developers on the same project and for them to view any changes you make, you need to push changes from your local branch to a remote repository. This leads us to remote branches. A remote branch refers to a branch that exists in a remote repository.


1 Answers

Nothing will "happen" to the branch, since a branch in git is just a lightweight, movable, disposable pointer. Commits are the real thing.

Yes, the overall plan is good, make a new branch to keep the most recent commits, reset master to where it should (I guess origin/master), and this ref on your C,D commits will allow them to stay permanently.

Without creating the branch, they would ultimately be garbage collected, even if they'd stay in the reflog for a while.

And also no, your C and D commits won't be merged into your upstream master if you follow your announced course of action. Go for it.

like image 180
Romain Valeri Avatar answered Sep 20 '22 19:09

Romain Valeri