- Continuous Integration,Delivery,and Deployment
- Sander Rossel
- 1138字
- 2021-07-02 15:42:17
Branching
Another major feature of Git is branching. With branches, you can create a copy of your current repository that is isolated from your main branch. So far, we've just committed everything to the default master branch, but you could make a new branch to develop certain features. Think of the many different versions of Linux. They are basically all different branches of the same master branch. Some branches even get their own branch. Ubuntu, for example, is a branch of Debian.
While Linux distributions separate from each other and grow in different directions, that probably does not make much sense for your own projects. Ultimately, you probably want to merge your work from one branch with that of another. Imagine working on some administrative software and your client requested a reporting feature and a means to send emails directly from the software. You are tasked with writing the reporting module while two of your coworkers are tasked with writing the email module. The rest of the team continues to work on other stuff.
You can now create two new branches, one for the reporting and one for the emailing module. This way, you and your other coworkers can safely commit any code without having to worry about breaking anything or deploying a half-finished reporting module. When you are finished with the reporting module, you can simply merge it into the main development branch.
You can create a new branch using the git branch branch-name command. Your branch name cannot contain spaces and it is generally considered a best practice to start your branch name with a letter, use lowercase only, use hyphens between words, and keep your branch names small. The git branch command will show a list of branches and it indicates your current branch with an asterisk (*). Finally, using the git checkout command, you can switch to another branch:
git branch reporting
git branch
git checkout reporting
Here is an example of some branches in Git GUI, found under the menu item Repository and then Visualize All Branch History:

Here, we have the master branch, the code branch, and the jarvis branch. In case you are unfamiliar with the Avengers movies (or comics), these are the guys trying to assemble an Iron Man suit. Apparently, Mr. Iron Man himself, Tony Stark, is taking care of the master branch while Black Widow and The Hulk are working on lasers and JARVIS, Iron Man's own really very cool Siri (he actually did it all himself as he is a rich genius, but for the example, let's say they are working together). Git GUI is not the prettiest tool out there, so here is another visualization of the same branch, but this time in GitKraken:

After merging the lasers and jarvis branches back to master, we get a pretty clear picture of what has happened. We will get back to merging in a minute:

You can also create branches off of branches. Simply check out the branch you want to use as a base and create a new branch there:
git branch weapons
git checkout weapons
git branch lasers
git checkout lasers
Things could now look as follows:

It is possible to remove branches. If a branch is merged into another branch and has no uncommitted changes, you can simply delete that branch using the -d switch. If a branch is not fully merged or has uncommitted changes, you must force delete it with the -D switch:
git branch -d branch-name
git branch -D branch-name
If you merged the branch with another, still existing, branch, the deleted branch will still show up in your branch overview. That makes sense as it is part of your history, whether the branch exists or not. Put differently, deleting the lasers and jarvis branches will not change the previous picture.
Once you create a branch, that branch only exists locally. That may be enough for your needs, for example, if you want to try out some stuff on a short-lived branch. Creating a branch, making changes, committing, and then pushing will not work. Once you push, you will get an error. Next, we will create a new branch, code (that is the big glowing orb in Iron Man's chest):
git branch reactor
git checkout reactor
git push
fatal: The current branch reactor has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin reactor
What --set-upstream origin reactor will do is tell your server to create a new branch where all your commits on this branch will go to. The shortcut for --set-upstream is -u, so that is what you will use:
git push -u origin reactor
When you go to GitLab and check out the branches tab on the project page, you can now see you have two branches. You will also see whether the branches are merged; you can compare branches, delete branches, and also create branches from here. Keep in mind that if you delete a branch here, it will still be present in your local working directory, but trying to push or pull will result in an error (unless you push with the -u switch, which will recreate the branch on your server).
The advantage of pushing your branch to the server is that you can share your branch with others. You will often have different branches, such as the development, test, and master (or production) branches, that everyone on the team will sooner or later commit to. Creating a branch to develop a single feature is often referred to as a feature branch. Feature branches can also be pushed to the server to have multiple developers working on the same feature. As an added bonus, if something happens to your computer, your branch is still on the server.
- 軟件安全技術(shù)
- C++程序設(shè)計(jì)教程
- UNIX編程藝術(shù)
- JavaScript百煉成仙
- Cocos2D-X權(quán)威指南(第2版)
- PHP基礎(chǔ)案例教程
- 量化金融R語言高級(jí)教程
- Yii Project Blueprints
- Getting Started with React Native
- 零代碼實(shí)戰(zhàn):企業(yè)級(jí)應(yīng)用搭建與案例詳解
- PHP+MySQL Web應(yīng)用開發(fā)教程
- Learning iOS Penetration Testing
- Distributed Computing with Python
- C語言王者歸來
- 軟件開發(fā)中的決策:權(quán)衡與取舍