What's the difference among "git fetch && git checkout" versus "git checkout" only?
Solution 1
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. :
git checkout develop
or whatever branch you needgit 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:
git checkout develop
git fetch && git pull
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.
Solution 2
git fetch
pulls references to the remote branches that are being created on (in this case) BItbucket.
git checkout
moves your current directory into a specific branch or a specific commit (depending the parameter).
What’s going on here? When you create a branch on your bitbucket, they provide you with that command because:
1- The branch was created on the REMOTE repository. Your local copy of the repository doesn’t have that new branch yet. That’s what the git fetch
command is used for.
2- Bitbucket assumes that, since you just created that new branch, you will work on it immediately and you need to tell that to your local repository. That’s what the git checkout
command is used for
That is not the only way to achieve it. You could, for example, aviod that git fetch
command by using:
git checkout -b {new_branch_name} && git pull origin {new_branch_name}
That’s not the most practical way, but probably will give you some better idea of how those commands work.
Solution 3
There is a problem with the question. In that it is missing a step. You actually need 3 steps to properly switch your working branch. If you just do the 2 steps described in your OP then you are in for some pain.
For a simple explanation with minimal jargon, if you want to locally work on a branch called devA
the 3 steps are:
Step 1: git fetch --all
although you could technically fetch just this one branch. It is a good idea to get into a habit of always doing fetch --all
This command makes git find out the state of the online repository actually is. It generally should be done before any operation and many people run scripts that aggressively do this automatically every minute.
For example, if you just try to go to step 2 without doing fetch all first, then it could very well return an error that said branch does not actually exist. Or switch to an outdated version of it and falsely inform you that you are up to date with the online repository when in fact you are not. Thus tricking you into thinking you do not need to do step 3
Step 2: git checkout devA
this just switches your git to work on that branch. simple.
Step 3: git pull
this actually updates your currently worked on branch (see step 2) to match the online repository. If you do not do this then next time you try to commit changes you will accidentally break stuff.
Although if it is the very first time you ever checkout a branch on the current machine you do not need to use this command.
With all that in mind, going back to the original question
git fetch && git checkout
= first find out what the state of the repository is. then switch to a branch.
git checkout
= without bothering to find out what the state of a repository is, try to switch to a branch. This could tell you the branch does not exist. Or it could switch to an outdated version of the branch while falsely telling you that it is up to date with the repository.
Solution 4
To be specific to your question " when doing a checkout from a branch in bitbucket they provide the command as: git fetch && git checkout develop ". This is because develop branch being created at cloud or remote , will not be available at you local machine until and unless you fetch the updates from remote.
Thus, to switch or checkout to develop branch , you first need to fetch all the remote updates to be aware of existence of develop branch. Once you do a fetch, your local reppo will be aware of the new branch being created at remote; and when you do a checkout to develop, it will setup a new local branch to track its remote couterpart.
Solution 5
git fetch
will pull down all changes from your remote location
git checkout
will switch you to a different branch (or restore your files to a previous state, depending how you use it)
Use fetch and checkout to switch branches and pull all updated files. Use only checkout to switch branches, but continue working on your local version.
Related videos on Youtube
mario ruiz
Software Engineer specialized on the web platform and email communications. Salesforce Marketing Cloud certified consultant and developer. Independent Consultant. I'm always looking for new opportunities to grow up, share knowledge, improve myself and taking new challenges in life! I believe in God, I’m husband and father. I've completed a Bachelor’s degree in Computer Sciences. I worked on the email platform and all sms campaigns. I'm an eager learner and encouraging myself to go beyond and apply the accurate tool for each job/requirement. I have been reviewer at Manning publications. I like to read js from many resources as I can: books like “Secrets of the JavaScript Ninja“ from John Resig (jQuery creator), “You Don’t Know JS” series. Thanks to these great people/companies that has shaped my career: Ethan Marcotte and Karen McGrane. Smashing Magazine book sand articles, A list apart articles, Lars Vogel, Pope Francis, Ben Nadel, Chris Coyier, Lea Verou, Marc Grabanski. And video courses from: frontendmasters.com, pluralsight.com, safaribooksonline.com and recorded or live stream Conferences. I feel comfortable also in Linux. I have the experience to recompile the kernel and install linux from scratch using floppy disks or from a LAN internet. My flavor is Debian/Ubuntu. I love Open Source. I consider linux as the root of the Open Source. I love the web revolution. Thanks for taking the time for read about myself. Read more on me on https://trailblazer.me/id?uid=mruiz35&cmty=trailhead https://www.linkedin.com/in/mario-ruiz-font http://twitter.com/marioruizfont “Go home and love your family” ― Mother Teresa
Updated on July 11, 2022