Cloud git tutorial – help developers make better use of GIT

Time:2022-1-1

Introduction: cloud effect git tutorial – help developers make better use of GIT. Git, as a distributed version control tool, has become more and more popular in recent years. Many companies and developers begin to use git to replace SVN. This article focuses on a deeper skill improvement tutorial on cloud effect for developers using git to help developers better use and understand the use of GIT.

Advanced learning materials

Customize your shortcut commands

There is a small skill in the cloud git tutorial that can make your git experience simpler, easier and familiar: alias. If you don’t want to enter the complete git command every time, you can easily set an alias for each command through git config. Here are some examples you can try

$ git config –globalalias.co checkout
$ git config –globalalias.br branch
$ git config –globalalias.ci “commit -s”
$ git config –globalalias.st status

This means that when you want to enter git commit – s, you only need to enter git CI. As you continue to use git, you may often use other commands, so don’t hesitate to create an alias. For example, to solve the ease of use problem of UN staging files, add your own UN staging alias to git:

$ git config –globalalias.unstage ‘reset HEAD –‘

This will be equivalent to the following two commands:

$ git unstage fileA $ git reset HEAD — fileA

It looks clearer. A last command is usually added, like this:

$ git config –global alias.last ‘log -1 HEAD’

In this way, you can easily see the last submission:

$ git last   
commit 66938dae3329c7aebe598c2246a8e6af90d04646 Author: Josh Goebel  <[email protected]> Date:   Tue Aug 26 19:48:51 2008 +0800      test for current head      Signed-off-by: Scott Chacon <[email protected]>

Code merging and rebasing

There are two main ways to integrate modifications from different branches in Git: merge and rebase.

merge

Cloud git tutorial - help developers make better use of GIT

The easiest way to consolidate branches is the merge command. It will triple merge the latest snapshots of the two branches (C3 and C4) and their nearest common ancestor (C2). The result of the merger is to generate a new snapshot (and commit)
Cloud git tutorial - help developers make better use of GIT

Rebase merge

You can extract the patches and modifications introduced in C4 and apply them once on the basis of C3. In git, this operation is called base changing. You can use the rebase command to move all changes submitted to one branch to another, just like “replay”.

In the above example, run:

Cloud git tutorial - help developers make better use of GIT

First, find the target branch of the current C2 and the target branch of the current C3. Then, compare the two branches to the base of the current C2. Then, the target branch of the current C2 and the target branch of the current C3 will be the same, Finally, the modifications previously saved as temporary files are applied in order.

Cloud git tutorial - help developers make better use of GIT

Now go back to the master branch and do a fast forward merge.

$ git checkout master
$ git merge experiment

Cloud git tutorial - help developers make better use of GIT

Merge branch code gracefully and submit

By comparing the two merging methods, rebase merging can make the submission history serial and look cleaner. So when merging code, some people like rebase to complete the merging. In addition, rebase has another function to modify the submission history. The following describes how to modify the submission history through rebase.

Elegant merge code is to modify the submission history through appropriate rebase, merge multiple submissions developed locally, and then rebase merge branches.

Code staging

Sometimes, after you’ve been working on a part of the project for a while, you want to switch to another branch and do something else. The problem is that you don’t want to create a submission for half the work just because you return to this later. The answer to this question is the GIT stash command.

Git stash saves unfinished changes to a stack, and you can reapply them at any time.

For demonstration, enter the project and change several files. Run git status to see the changed status:

Cloud git tutorial - help developers make better use of GIT

If you want to switch branches but do not want to commit, run git stash or git stash save:

Cloud git tutorial - help developers make better use of GIT
The working directory is clean, and the changes just made no longer exist:

$ git status
On branch master nothing to commit, working directory clean

At this point, you can easily switch branches and work elsewhere; Your changes are stored on the stack. To view temporary items, you can use git stash list:

Cloud git tutorial - help developers make better use of GIT

In this case, there are two previous staging tasks, so you are exposed to three different staging tasks. You can reapply the work you just temporarily saved through the command in the help prompt of the original stash command: git stash apply. If you want to apply one of the older staging, you can specify it by name, such as git stash apply stash @{2}. If you do not specify a staging, GIT thinks that the most recent staging is specified:

Cloud git tutorial - help developers make better use of GIT

You can see that git has modified the files that were undone when you staged them. You can also run git stash pop to apply staging and throw it off the stack.
Discard of staging can be removed by running git staging drop plus the name of the staging to be removed:

Cloud git tutorial - help developers make better use of GIT

Create a branch from staging

If some work is staged, there may be problems when reapplying work later. If the application tries to modify the file just modified, you will get a merge conflict and have to resolve it. If you want an easy way to test the stored changes again, you can run git stash branch to create a new branch, check out the submission where the temporary work is located, apply work there again, and then throw it away after the application is successful:

Cloud git tutorial - help developers make better use of GIT

This is a good way to easily resume staging and continue working in a new branch.

Modify code history

Many times, when using git, you may want to modify the submission history for some reason. The great thing about Git is that it allows you to make decisions at the last minute. You can override submissions that have occurred as if they had occurred in another way. This may involve changing the order of submission, changing the information in the submission or modifying the file, compressing or splitting the submission, or completely removing the submission – before sharing your work with others.

In this section, you can learn how to complete these very useful work, so that your submission history will be displayed as you wish when sharing your work with others.

Modify last submission

Modifying your last submission is probably the most common of all operations to modify historical submissions. For your last submission, you often want to do two things: modify the submission information, or modify the snapshot of the files you added, modified and removed.
If you just want to modify the last submitted information, it is simple:

$ git commit –amend

This will take you into a text editor that contains your latest submission for you to modify. When you save and close the editor, the editor will replace the latest submission with your input.

If you have completed the submission and forgot to add a newly created file during the previous submission and want to change the submitted snapshot by adding or modifying the file, you can also complete similar operations. Modify the file, then run git add or git RM to a tracked file, and then run git commit — amend to take the current staging area and make it a newly committed snapshot.

You need to be careful when using this technique, because the correction will change the submitted SHA-1 checksum. It’s similar to a small variable base – don’t fix it if you’ve pushed the last commit.

Modify multiple submission information

In order to modify submissions that are farther in the submission history, more complex tools must be used. Git does not have a change history tool, but you can use the rebase tool to rebase a series of submissions based on their original head instead of moving them to another new one. With the interactive rebase tool, you can stop after any submission you want to modify, and then modify information, add files, or do whatever you want. You can interactively run rebase by adding the – I option to git rebase. You can specify how long you want to override the history base.

For example, if you want to modify the last three submissions or any one of those submissions, pass the parent submission of the last submission you want to modify as a parameter to the GIT rebase – I command, that is, head ~ 2 ^ or head ~ 3. Remember that ~3 may be easier because you are trying to modify the last three submissions; Note, however, that the previous four submissions are actually specified, that is, the parent submission that you want to modify the submission:

$ git rebase -i HEAD~3

Remember again that this is a variable base command – in head ~ 3 Every submission within the head scope will be rewritten, whether you modify the information or not. Don’t involve any submissions that have been pushed to the central server – doing so will confuse others by producing two versions of a change at once. Running this command will give you a submitted list in the text editor, which looks like the following:

Cloud git tutorial - help developers make better use of GIT

It should be noted that these submissions are displayed in the opposite order to the log command normally used. Run the log command once and you will see something like this:

Cloud git tutorial - help developers make better use of GIT

Note the display in reverse order. Interactive rebasing gives you a script that it will run. It will start with the submission (head ~ 3) you specify on the command line, and repeat the changes introduced by each submission from top to bottom. It lists the oldest, not the latest, because that will be the first to repeat.

You need to modify the script to make it stay on the changes you want to modify. To achieve this goal, you just need to change the ‘pick’ in front of each submission you want to modify to ‘Edit’. For example, if you only want to modify the third submission information, you can modify the file as follows:

edit f7f3f6d changed my name a bit
pick 310154e updated README formatting and added blame
pick a5f4a0d added cat-file

When you save and exit the editor, GIT takes you back to the last submission in the list, sends you back to the command line and prompts the following message:

Cloud git tutorial - help developers make better use of GIT

These instructions tell you exactly what to do. input

$ git commit –amend

Modify the submission information and exit the editor. Then, run

$ git rebase –continue

This command will automatically apply the other two submissions, and then it is completed. If you need to change more than one pick to edit, you need to repeat these steps on each submission changed to edit. Each time, GIT will stop, let you fix the submission, and then continue until it is completed.

Reorder submissions

You can also use interactive rebasing to reorder or completely remove submissions. If you want to remove the “added cat file” submission and modify the order in which the other two submissions are introduced, you can change the base script from the following:

pick f7f3f6d changed my name a bit
pick 310154e updated README formatting and added blame
pick a5f4a0d added cat-file

Replace with the following:

pick 310154e updated README formatting and added blame
pick f7f3f6d changed my name a bit

When you save and exit the editor, GIT takes your branch back to the parent submission of these submissions, applies 310154e, then applies f7f3f6d, and finally stops. The fact changes the order of those submissions and completely removes the “added cat file” submission.

Compressed submission

With the interactive rebase tool, you can also compress a series of submissions into a single submission. In the variable base information, the script gives useful instructions:

Cloud git tutorial - help developers make better use of GIT

If you specify “square” instead of “pick” or “Edit”, GIT will apply the modifications of both and merge the submission information together. Therefore, if you want these three submissions to become one submission, you can modify the script as follows:

pick f7f3f6d changed my name a bit squash
310154e updated README formatting and added blame squash
a5f4a0d added cat-file

When you save and exit the editor, GIT applies all three modifications and then puts you in the editor to merge the three submissions:

Cloud git tutorial - help developers make better use of GIT

Split commit

Splitting a submission will undo the submission, and then partially stage and submit multiple times until you complete the required number of submissions. For example, suppose you want to split the middle of three submissions. Want to split it into two submissions:
The first “updated readme formatting”,
The second “added blank” replaces the original “updated readme formatting and added blank”. You can do this by modifying the script of rebase – I. modify the submitted instruction to be split to “Edit”:

pick f7f3f6d changed my name a bit edit
310154e updated README formatting and added blame pick
a5f4a0d added cat-file

Then, when the script takes you to the command line, reset the submission, get the reset changes, and create several submissions from it. When you save and exit the editor, GIT takes you to the parent submission of the first submission in the list, applies the first submission (f7f3f6d), applies the second submission (310154e), and then lets you enter the command line. There, you can make a mixed reset for that submission through git reset head ^ to actually undo that submission and leave the modified file not temporarily stored. You can now stage and commit files until there are several commits, and then run git rebase — continue when it is complete:

Cloud git tutorial - help developers make better use of GIT

Git applies the last commit (a5f4a0d) in the script, and the history looks like this:

Cloud git tutorial - help developers make better use of GIT

Again, these changes change the SHA-1 checksum of all submissions in the list, so make sure that the submissions in the list have not been pushed to the shared warehouse.

Graphical Git

TortoiseGit

Support system: windows (free software)
There are several ways to install git on windows. More versions can be found inhttps://tortoisegit.orgDownload.

SourceTree
Support system: Windows / OS X (free software)
https://www.sourcetreeapp.com

Git plug-in egit for eclipse
Git plug-in egit for eclipse (optional)
The plug-in installation of eclipse is not described here.
Egit plug-in address:http://download.eclipse.org/e…
Here are the egit learning materials:http://www.cnblogs.com/hnrain…

Git plug-in on IntelliJ idea
Git plug-in on IntelliJ idea (optional)
Installed by default.

Git GUI

Git GUI is git’s official graphical interface, and its strength is browsing and selecting code changes before commit. It can run on windows and Linux.

Gitk

Gitk is also the official graphical interface of GIT. Its strengths are to display version tree and historical information. It can run on windows and Linux.

summary

Cloud effect git tutorial – help developers make better use of GIT. Git has become more and more popular among developers in recent years. This article focuses on the summary of common skills to help you make better use of GIT, andCloud code management codeupAs an enterprise level code management platform produced by Alibaba cloud, it provides functions such as code hosting, code review, code scanning and quality detection, comprehensively protects enterprise code assets, and can better help enterprises achieve safe, stable and efficient R & D management.