Jump to content

Git - super confused, please help

Hi P
Go to solution Solved by elpiop,

Your branch stays until you delete it. Merging `myBranch` into master just takes the commits that you made on `myBranch` and applies them on top of master. You could continue working off of `myBranch` and make some more commits, then merge those in to master again at a later date.

 

If you checked out the project, you started on the master branch. Then, you branched off of it and made changes on `myBranch` and merged them back into master. Since you applied all the changes that you made on `myBranch` onto the master branch, the master and `myBranch` should be identical, as you said they are. (see image)

Image result for git merge master

 

`git pull` fetches all commits from the remote (on the branch you have checked out) and merges them into your local branch.

 

We did the following

  • Someone created the base of the project
  • Created a GitHub repository for it
  • Pushed everything to Master

 

Then I did:

  • Cloned the repository
  • Made a branch
  • Worked on it (commiting and pushing)
  • Sent pull request on GitHub

The merge was successful but my branch still exists, do they not disappear or something after merge?

Then I realized both my branch and the master are exactly the same (files, directory, code, everything), what...?

 

Questions:

  1. Why do branches continue existing after merging them?
  2. Why did my branch copy everything from Master after merging?

 

On hindsight... I do recall doing always the following:

 

> git branch (just to double check where I am)

> git checkout myBranch

> git pull

 

Does "git pull" on a branch pulls everything including master into it?

should I've done "git pull origin myBranch" instead?

 

Thank you very much, I'm confused as hell :)

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Your branch stays until you delete it. Merging `myBranch` into master just takes the commits that you made on `myBranch` and applies them on top of master. You could continue working off of `myBranch` and make some more commits, then merge those in to master again at a later date.

 

If you checked out the project, you started on the master branch. Then, you branched off of it and made changes on `myBranch` and merged them back into master. Since you applied all the changes that you made on `myBranch` onto the master branch, the master and `myBranch` should be identical, as you said they are. (see image)

Image result for git merge master

 

`git pull` fetches all commits from the remote (on the branch you have checked out) and merges them into your local branch.

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 11/15/2019 at 12:59 AM, Hi P said:

Then I realized both my branch and the master are exactly the same (files, directory, code, everything), what...?

Why did my branch copy everything from Master after merging?

If you branch away from master, do some changes and then merge those changes back into master, I would very much expect your branch and master to be identical at this point in time, unless additional changes were merged into master in the meantime (e.g. some other branch).

 

With that being said, it is often advisable to merge master into your branch, before merging your branch into master. This will update your branch with any changes that were added to master in the meantime, before your changes are added into master. This is done to check for and, if needed, resolve merge conflicts before merging your branch into master.

- git checkout master           # switch to master branch
- git pull                      # update local copy of master to match remote
- git checkout <your-branch>    # switch back to your branch
- git merge master              # merge master into your branch
On 11/15/2019 at 12:59 AM, Hi P said:

> git branch (just to double check where I am)

This works, but the more appropriate command would be "git status". This will not only tell you where you are, but also if you have uncommited changes and changes you haven't pushed yet.

 

GitHub actually has an option to automatically delete a branch after a merge request (or "pull request" in their lingo) is accepted. Other sites, like GitLab, have similar options.

 

You should also know that deleting a branch on remote (i.e. "origin") will not automatically remove the branch from your machine. In fact, you machine will continue to think the remote branch exists, until you update that information. This is done with "git remote prune origin".

 

To remove a branch from remote, you can do "git push origin --delete <branch-name>". Do delete a merged branch on your local machine you can do "git branch -d <branch-name>". If the branch hasn't been merged yet, you need to use "-D".

 

On 11/15/2019 at 12:59 AM, Hi P said:

Does "git pull" on a branch pulls everything including master into it?

should I've done "git pull origin myBranch" instead?

A "git pull" will fetch the changes from the remote copy of your branch and merge those changes into your local branch. In fact, "git pull" is just a combination of the commands "git fetch" and "git merge" combined into one. You can use those two commands to perform these operations separately, e.g. if you want to have a look at the changes before merging them.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×