官术网_书友最值得收藏!

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.

When you first create a Git repository, it will not have any branches by default. You need to add a file and commit it to initialize the master branch. If you have nothing to add to master, because you want to use feature branches, just add a readme, license, or .gitignore file, which can all be added directly from the GitLab project page. Only after you have created your master branch can you create new branches. You can rename your master branch after you have created it if you want, but I really see no reason for that.

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.

The origin is simply a local alias to a URL that Git creates. In this case, the origin points to http://ciserver/the_avengers/the-suit.git. So what you are actually saying is push my commits to the Git URL on the specified branch. Every Git repository has an origin by default. You can check your remote aliases using the git remote -v command and you can rename your URLs using git remote rename origin somethingelse.
主站蜘蛛池模板: 吉隆县| 南昌市| 罗山县| 甘泉县| 汪清县| 绍兴市| 锦屏县| 玉树县| 炎陵县| 永善县| 永登县| 唐海县| 自治县| 武安市| 龙里县| 聊城市| 靖远县| 乐山市| 淮安市| 喀喇沁旗| 抚顺县| 富宁县| 桐乡市| 尉犁县| 龙南县| 凤城市| 华蓥市| 株洲县| 刚察县| 华宁县| 峨边| 彭州市| 庆城县| 襄汾县| 于都县| 马山县| 兴海县| 乌海市| 香河县| 灵璧县| 台山市|