1

When working with git, before starting work on a feature or an issue, I would do following steps

git checkout master // make sure you are on local master

git fetch origin // get latest commits from remote

git merge origin/master // merge latest commits from remote into local master

git checkout -b new-branch // create new branch from up-to-date remote master

However, I am reading Atlassian tutorial for feature-branch workflow, and they suggest doing following

git checkout master

git fetch origin

git reset --hard origin/master

git checkout -b new-branch

Obviously, except step 3, we follow identical steps. But as new git user, I am confused with step 3 even after I read explanation of what it does.

What is different btw my and their step 3? Which and why is better?

0

3 Answers 3

1

The example you show is from Atlassian's Git Feature Branch Workflow Under normal circumstances, after a fetch, there is not difference between git merge origin/master and git reset --hard origin/master.

However, reset --hard is destructive and I would avoid executing it routinely. merge is something you could undo if you execute it by accident in a wrong branch, or at a different moment.


Simpler way

BTW, you can just

git checkout master
git pull 
git checkout -b feature_branch
1

Have a look into this post

Briefly:
- git merge apply all unique commits from branch A into branch B in one commit with final result
- git merge doesn’t rewrite commit history, just adds one new commit
- git rebase gets all unique commits from both branches and applies them one by one
- git rebase rewrites commit history but doesn’t create extra commit for merging

1

Your question is a bit wrong!

git reset --hard origin/master

is not rebase, it's hard reset; see also Reset Demystified.

In your case it means "remove all local changes in the current branch and move the branch pointer to origin/master". The advantage of it is the command completely synchronizes your local branch with remote; the disadvantage is that it removes your local commits and uncommitted changes if there were any.

Not the answer you're looking for? Browse other questions tagged or ask your own question.