Can I do this in Git (Update a branch without any problems)?

Viewed 33

When I was new in the company, when doing a task, I was doing

    git checkout master
    git pull
    git checkout -b BRANCH
    git push -u origin BRANCH

to create a new branch, then I would do the task and do the usual

    git add .
    git commit -m MESSAGE
    git push

and create PR, now, imagine I did this 3 months ago, so going back to the branch and updating it will give me all sort of problems ( I need to update to origin/candidate, then merge ... )

Would it be much cleaner if I can just delete the BRANCH with

    git branch -D BRANCH
    (also delete the remote BRANCH)

I want to (from an old branch, update the branch, then merge, but can I do that without the merging problems?) Just do the whole process again? (from the above?) which will save me from the updating of the branch and merging? Is that going to be a more clean process? Or you would suggest (other) alternatives?

1 Answers

tl;dr: Yes, you can update without any problems (compared to starting over).

Details:

Paraphrasing your question:

Is starting over with a new up to date branch and re-creating the previous commits (either manually, or with cherry-picks, etc) going to be a more clean process than updating the existing branch?

Before I answer, first note that instead of starting over, two common ways to update your branch are:

  1. Merge in the latest master branch into your branch.
  2. Rebase your branch onto the latest master.

And now the answer: If you elect to start over with a fresh up to date branch, this would be identical to updating your existing branch with rebase. The only difference is that starting over would take slightly longer.

Using the names from your question, let's call your branch BRANCH, your upstream origin, and your target branch for a PR master. Here are the 2 ways to update your branch:

# Update BRANCH with the latest master using merge
git fetch
git checkout BRANCH
git merge origin/master

# Update BRANCH with the latest master using rebase
git fetch
git rebase origin/master BRANCH

Note the rebase is providing a second argument of BRANCH which works even if you don't have BRANCH checked out yet. (If you already have BRANCH checked out you can leave that off.) The end result of the rebase will be identical to creating a new branch and cherry-picking your old commits. In fact, behind the scenes that's basically what a rebase does.

Side Note: you don't even need to keep a local copy of master anymore. I would recommend deleting your local master and possibly never checking it out again! The above commands don't use it, but instead use origin/master. You also don't need a local master for creating a new branch:

git fetch
git switch -c BRANCH origin/master --no-track

That creates a new branch named BRANCH starting with origin/master and says not to track it, because by default, when you create a branch from origin/* it will track it unless you tell it not to.

Related