John Cordero John Cordero - 3 months ago 7
Git Question

Git Branching trouble query with terminal

I know these questions are simple but I'm having problems with branching:

When I make a new branch, am I supposed to download separate files or use that same folder to make changes? If so how do I? Just a git clone?

How do I push commits to another branch?

Would it be just git commit blah blah?
Git push origin branch name?

Answer

Q1) When I make a new branch, am I supposed to download separate files or use that same folder to make changes?

Let's say you are on origBranch (you can check with git branch) and you want to create newBranch

    git checkout -b newBranch

Now you are on newBranch which is the same as origBranch. Any changes committed will now be in only newBranch.

Q2) How do I push commits to another branch? Would it be just git commit blah blah Git push origin branch name.

If you would like to push just the newBranch, it's

    git push -u origin newBranch

If you would like to add commits from newBranch in to origBranch, there are several approaches.

Merge approach:

    git checkout origBranch
    git merge newBranch
    git push

Rebase Approach:

    git checkout newBranch
    git rebase origBranch

Cherry pick approach:

    git checkout origBranch
    git cherry-pick <commit_id from newBranch>
    git push

Pull request approach:

  1. Go to repro on git hub
  2. Select new pull request
  3. From the base drop down, select origBranch
  4. From the compare drop down, select newBranch
  5. Add in notes or assignees to review
  6. Select Create Pull Request
Comments