$mkdirtest-repositories$cdtest-repositories/$test-repositories
The preceding example creates the test-repositories directory using the mkdir (make directory) command and switches to that directory using the cd (change directory) command.
5. Clone the forked repository into the directory you just created. It might look something like this:
$gitclonehttps://dstevenstest@bitbucket.org/dstevenstest/mygittutorial.bitbucket.io.gitCloninginto'mygittutorial.bitbucket.io'...remote:Countingobjects:12392,done.remote:Compressingobjects:100%(12030/12030),done.remote:Total12392(delta8044),reused564(delta360)Receivingobjects:100%(12392/12392),2.72MiB|701.00KiB/s,done.Resolvingdeltas:100%(8044/8044),done.$cdmygittutorial.bitbucket.io/
This clones the repository using thegit clonecommand and creates the directory the clone createdmygittutorial.git.bitbucket.io.
Create a branch and change something using the branching workflow
You're going to add a quote on your website in this branch.
1. Create a branch using thegit branchcommand.
$gitbranchtest-1
2.Check out the branch you just created using thegit checkoutcommand.
$gitcheckouttest-1Switchedtobranch'test-1'
3.List the branches you have locally using thegit branchcommand.
$gitbranchmain*test-1
4.Make an update to theeditme.htmlfile by adding a quote. You can use something like the following:
Thisisaquote,andIlikeit.
Aquote:TheArtofQuoting
5. Add that change.
gitaddeditme.html
Note: Your change isn't committed to the git history yet – it's in a "waiting" state. We learned about this inSaving changes.
6. Commit the change with a descriptive commit message.
gitcommiteditme.html-m'addedanewquote'[test-1063b772]addedanewquote1filechanged,3insertions(+),3deletions(-)
Note: Now the change is part of the git history as a single "commit."We learned about this inSaving changes.
7. Push that change to Bitbucket using the git push command.
gitpushfatal:Thecurrentbranchtest-1hasnoupstreambranch.Topushthecurrentbranchandsettheremoteasupstream,usegitpush--set-upstreamorigintest-1
You will see an error because the first time you push a new branch you created locally you have to designate that branch.
8. Push the branch and change using thegit push branchcommand.
$gitpushorigintest-1Countingobjects:3,done.Deltacompressionusingupto8threads.Compressingobjects:100%(3/3),done.Writingobjects:100%(3/3),363bytes|0bytes/s,done.Total3(delta2),reused0(delta0)remote:remote:Createpullrequestfortest-1:remote:https://bitbucket.org/dstevenstest/dans.git.bitbucket.org/pull-requests/new?source=test-1&t=1remote:Tohttps://bitbucket.org/dstevenstest/dans.git.bitbucket.org.git*[newbranch]test-1->test-1
This tells the system that the origin repository is the destination of this new branch.
9. Open your tutorial repository and click Branches. You should now see both the mainand the test-1 branches. It should look something like this:
Create, fetch, and checkout a remote branch
When you're working in a team you'll likely have to pull or fetch branches which other team members create and push to Bitbucket. This example will give you some of the basics of creating and working with branches others create.
1. Go to your tutorial repository in Bitbucket and clickBranches. You should see something like this:
2. ClickCreate branch, name the branchtest-2, and clickCreate.
3. Copy thegit fetchcommand in the check out your branch dialog. It will probably look something like this:
$gitfetch&&gitcheckouttest-2Fromhttps://bitbucket.org/dstevenstest/dans.git.bitbucket.org*[newbranch]test-2->origin/test-2Branchtest-2setuptotrackremotebranchtest-2fromorigin.Switchedtoanewbranch'test-2'
4. Use thegit branchcommand in your terminal. You should see a list of branches something like this:
$gitbranchmaintest-1*test-2
The branch with the asterisk * is the active branch. This is critical to remember when you are working in any branching workflow.
5. Use thegit statuscommand and you'll see something like this:
$gitstatusOnbranchtest-2Yourbranchisup-to-datewith'origin/test-2'.nothingtocommit,workingtreeclean
You can see what branch you're on and that the branch is currently up to date with your remote (origin) branch.
6. Use thegit checkoutcommand to change the focus back to your other branch. The command will look something like this:
$gitcheckouttest-1Switchedtobranch'test-1'Yourbranchisaheadof'origin/test-1'by3commits.(use"gitpush"topublishyourlocalcommits)
One of the most important things to remember when working in branches is that you want to be sure the branch you're making changes to is the correct branch.
Push change and create a pull request
Now it's time to get your first change reviewed and merge the branch.
1. Click+> Create a pull request. You can see yourtest-1branch as the source branch andmainin the destination branch.
Because we created this repository by forking an existing repository the destination is set to the mainbranch of the repository we forked.
To correct this you will need to change the repository destination branch (the branch into which you will merge your changes) from tutorials/tutorials.git.bitbucket.org to your repository.
You would also add reviewers on your team to the pull request. Learn more about pull requests
2. ClickCreate pull request.
3. Make a comment in the pull request by selecting a line in the diff (the area displaying the change you made to theeditme.htmlfile).
4. ClickApprovein the top left of the page. Of course in a real pull request you'd have reviewers making comments
5. ClickMerge.
6. (Optional) Update theCommit messagewith more details.
7. Select theMerge commitMerge strategyfrom the two options:
- Merge commit—Keeps all commits from your source branch and makes them part of the destination branch. This option is the same as entering git merge --no-ff in the command line.
- Squash—Combines your commits when you merge the source branch into the destination branch. This option is the same as entering git merge --squash in the command line.
Learn more for details on these two types of merge strategies.
8. ClickCommitsand you will see how the branch you just merged fits into the larger scheme of changes.
Delete a branch and pull main into local working branch
Now you've gone through the basic branching workflow and your change is in main. The last thing we'll learn is how to delete the branch you just merged, pull the updated mainbranch, and merge the updated mainbranch into yourtest-2branch.
Why delete the branch?
Remember, branching in Git differs from SVN or similar version control systems by using a branches as both long running branches, like a mainand development branch, and short term development branches like the examples we use in this tutorial. Because this is the case it's not a bad idea to delete local branches to keep your local environment cleaner.
Why pull mainand merge it into test-2?
We're using this as an example of you working on a repository into which another team member is working. It's a good idea to pull changes into your working branch from time to time to prevent merge conflicts in pull requests.
1. Open your terminal and run thegit statuscommand. The result should look something like this:
$gitstatusOnbranchtest-1nothingtocommit,workingtreeclean
You can see you're on the branch you just used to make your change and that you don't have any changes. We're ready to get rid of that branch now that we've finished that work.
2. Switch to the mainbranch by running thegit checkout maincommand. The result should look something like this:
gitcheckoutmainSwitchedtobranch'main'Yourbranchisup-to-datewith'origin/main'.
Notice that the message says you are up-to-date? This is only your local branch. We know this because we just merged a change into mainand haven't pulled that change from the remote repository to our local system. That's what we'll do next.
3. Run thegit pullcommand. The result should look something like this:
$gitpullremote:Countingobjects:1,done.remote:Total1(delta0),reused0(delta0)Unpackingobjects:100%(1/1),done.Fromhttps://bitbucket.org/dstevenstest/dans.git.bitbucket.org2d4c0ab..dd424cbmain->origin/mainUpdating2d4c0ab..dd424cbFast-forwardeditme.html|6+++---1filechanged,3insertions(+),3deletions(-)
What happened is that when you pull the changes from the remote repository git runs a fast-forward merge to integrate the changes you made. It also lists how many files and lines in that file changed.
4. Run thegit branch -d {branch_name}command to remove thetest-1branch. The result will look something like this:
$gitbranch-dtest-1Deletedbranchtest-1(was063b772)
You can see that it deleted the branch and what the last commit hash was for that branch. This is the safe way to delete a branch because git won't allow you to delete the branch if it has uncommitted changes. You should be aware however that this won't prevent deleting changes which are committed to the git history but not merged into another branch.
5. Switch to thetest-2branch using thegit checkoutcommand.
$gitcheckouttest-2Switchedtobranch'test-2'Yourbranchisup-to-datewith'origin/test-2'.
6.Merge the mainbranch into your working branch using thegit merge maintest-2command. The result will look something like this:
$gitmergemaintest-2Updating2d4c0ab..dd424cbFast-forwardeditme.html|6+++---1filechanged,3insertions(+),3deletions(-)
It's important to remember the following:
- The active branch matters. If you want to merge maininto test-2, you need to have test-2 checked out (active). The same is true if you want to merge test-2 into main– you need to have mainchecked out.
- To see what branch is active at any time, usegit branchand the active branch will have an asterisk. Or usegit statusand it will tell you want branch you are on and if there are pending local changes.
We hope you've learned a bit about branching and the commands involved. Let's review what we just covered:
Review the branching workflow
The Git Feature Branch workflowis an efficient way to get working with your team in Bitbucket.In this workflow, all feature development takes place on branches separate from the mainbranch. As a result, multiple developers can work on their own features without touching the main code.
Start with themainbranch
This workflow helps you collaborate on your code with at least one other person. As long as your Bitbucket and local repos are up-to-date, you're ready to get started.
Create anew-branch
Use a separate branch for each feature or issue you work on. After creating a branch, check it out locally so that any changes you make will be on that branch.
Update, add, commit, and push changes
Work on the feature and make commits like you would any time you use Git. When ready, push your commits, updating the feature branch on Bitbucket.
Get your code reviewed
To get feedback on your code, create a pull request in Bitbucket. From there, you can add reviewers and make sure everything is good to go before merging.
Resolve feedback
Now your teammates comment and approve. Resolve their comments locally, commit, and push changes to Bitbucket. Your updates appear in the pull request.
Merge your branch
Before you merge, you may have to resolve merge conflicts if others have made changes to the repo. When your pull request is approved and conflict-free, you can add your code to the mainbranch. Merge from the pull request in Bitbucket.
This tutorial is limited in its ability to show how branches make teams more effective. There are several approaches to branching and we discuss some of these approaches in:Comparing workflows.