Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference among "git fetch && git checkout" versus "git checkout" only?

Tags:

Should do we always do as:

git fetch && git checkout 

Or only,

git checkout 

?

For example when doing a checkout from a branch in bitbucket they provide the command as:

enter image description here

git fetch && git checkout develop 

But why is this necessary if

git checkout

will do the same, isn't it?

like image 547
mario ruiz Avatar asked Jun 13 '18 18:06

mario ruiz


People also ask

What is difference between fetch and push in git?

Pushing is how you transfer commits from your local repository to a remote repository. It's the counterpart to git fetch but whereas fetching imports commits to local branches, pushing exports commits to remote branches.

What is a git fetch?

Git fetch summary In review, git fetch is a primary command used to download contents from a remote repository. git fetch is used in conjunction with git remote , git branch , git checkout , and git reset to update a local repository to the state of a remote.

What is the difference between git fetch and git remote update?

git remote update will update all of your branches set to track remote ones, but not merge any changes in. git fetch will update only the branch you're on, but not merge any changes in. git pull will update and merge any remote changes of the current branch you're on.

Is git pull the same as fetch and merge?

The git pull command first runs git fetch which downloads content from the specified remote repository. Then a git merge is executed to merge the remote content refs and heads into a new local merge commit.


1 Answers

To chime in here since I have to use Bitbucket daily for multiple projects and multiple branches I will give you my recommendation.

If you checkout from Bitbucket, i.e. create a branch, then you should be ok using the commands that they have provided as you pasted in your example. However, since it is likely that after the initial checkout you will be switching branches, creating branches and your local will get out of sync I recommend the following using your terminal. :

  1. git checkout develop or whatever branch you need
  2. git fetch && git pull i.e. fetch all branches and latest changes as well as pull in all changes from the branch you are on.

Yes this does seem like duplicate work but working with Bitbucket I will say that this is the safest and sanest way to ensure that you have the latest from the branch that you are working on.

That being said, you should always create branches and never push directly to your develop or master branches.

So let's say that you are on develop branch and you have done the above by checking out the branch and have fetched and pulled the latest you would then create a branch off of that main branch using standard git checkout -b my-feature-branch

Example of what we do at my shop:

  1. git checkout develop
  2. git fetch && git pull
  3. git checkout -b feature/JIRA_ISSUE_NUMBER-update-layout-for-this-page

Now you have checked out the develop branch, pulled down all the latest changes and remote branches and created a feature branch from that develop branch.

Hope this helps.

like image 84
isaac weathers Avatar answered Oct 09 '22 23:10

isaac weathers