introduction
Git is an excellent version control system. If there is no actual application scenario, beginners may feel very difficult just learning git related commands. Therefore, organize a tutorial to introduce the basic use process of git in different scenarios.
1: Can do skilled management code locally.
Xiao Ming is a programming ape and likes to participate in various programming competitions. In the process of participating in the competition, Xiao Ming wrote a lot of code, which are saved in the integrated development environment. With the progress of the competition, Xiao Ming encountered several headache problems:
- Question 1: in order to improve the ranking of the competition, Xiao Ming tried three different algorithms. He only needs to call one algorithm at a time and use different function calls to execute the corresponding algorithm. With the increasing amount of code, the code is becoming more and more bloated. He wants to put these three schemes in three projects independently, but it's too troublesome..
- Question 2: Xiao Ming found that the score of the code in recent days is not as good as the code submitted a week ago, but the code has changed so much. How can he return to the code a week ago?
- Question 3: Xiao Ming is working hard to write the code every day, but there is no place to record the improvement of the code every day. In this way, he has been busy for several days and it is difficult to summarize and reflect...
Xiao Ming searched Baidu and finally found a powerful tool - Git; From then on, Xiao Ming began to explore Git's wonderful journey.
Introduction to Git:
Git is an open source distributed version control system, but Xiao Ming doesn't understand these. As long as this tool can manage its own code.
By checking the information on the Internet, Xiao Ming finally installed the git tool.
Xiao Ming starts to initialize his first git warehouse, creates a folder and executes it
git init
Xiao Ming put two code files into git warehouse, a.c and B.C;
Although the file is placed under the git directory, it has not been incorporated into the version control of git and cannot be executed
git add a.c git add b.c git commit -m "This is xiaoming's frist git !!!"
- Scene: Xiao Ming thinks hard and comes up with a new scheme in the competition. He wants to build the code from scratch. In this case, do you need to rebuild an engineering project? No, because Xiao Ming uses git.
So Xiao Ming created a new branch
git checkout -b testing # Equivalent to the following git branch testing git checkout testing # If you want to delete a branch git branch –d Branch name
- Scenario: Xiao Ming submits several commit s, regrets and wants to return his submission record:
git log Command to view the branch submission history and confirm the version that needs to be rolled back git reset --hard commit_id Command to perform version fallback git push origin Command, push to remote branch
If you want to go back to the previous version directly, you can use the shortcut command
git reset --hard HEAD^
Xiao Ming also found that there are two ways to return the code: reset and revert. Here are the differences:
In fact, to fallback code, both of them can be used. You can select different commands according to your needs.
-
If you want to roll back the code to a commit at a certain point in time, you can actually reset and revert, but reset is more convenient
-
If you want to roll back only one commit in the middle, you need to use revert, d
git log View history of submissions git revert -n 97ea0f9 git commit -m "Restore the third modification"
- Scenario: Xiao Ming wants to add a log in all branches_ info "XiaoMing is very clever!"
In this case, Xiaoming should be able to switch to each branch and add logs manually. However, because Xiaoming uses git, all use its powerful cherry pick function.
# First cut to the branch to be modified git log Query history submission record git cherry-pick <HashA> <HashB>
- Scenario: Xiao Ming has submitted a large number of good commit s in the branch and thinks that these codes can be integrated into the master branch. At this time, you can use merge (is this a bit like cherry pick?)
git checkout dev git pull git checkout master git merge dev git push -u origin master
2: Start with a remote warehouse
Xiao Ming can skillfully manage his own code by using git, but with it, Xiao Ming thought that my code is only saved locally now. If you can submit the code you wrote to the Internet for saving, you won't be afraid of the computer hanging up and losing your code, so Xiao Ming began to take action.
First, you need to configure the git global user name and user mailbox (in fact, Xiao Ming doesn't know why to configure this)
git config --global user.name "your username" git config --global user.email "your Email"
Before pushing the warehouse, you also need to add an SSH secret key
ssh-keygen -t rsa -C "Yours email" cat ~/.ssh/id_rsa.pub Then copy the secret key to the remote SSH Just put it in the secret key.
Add remote warehouse:
git remote add origin ssh://git@codehub-dg-y.huawei.com:2222/w00569886/Gauss-knowledge.git
Xiao Ming pushes the code to the remote end
git push -u origin master # With the - u parameter added to the above command, an association will be made, and subsequent push or pull will be much more convenient.
Summary of commands to delete branches
With the increasing number of branches, Xiao Ming found that some branches were unnecessary. To clean them up, he carried out the following operations:
# Let's see what branches there are git branch -a # To delete a branch of a remote warehouse, execute the following command: git push origin -- delete Branch name # Delete local branch git branch –d Branch name # for instance: git branch -d 1.5.3-issue-exporter git push origin --delete 1.5.3-issue-exporter git push origin --delete 1.5.3-issue-wsr
3: Collaborative code development using git
Xiao Ming joined Xiao Zhang in the process of the competition, so they wrote code together and worked together. But how can the code written by two people be combined? Manually copy functions one by one? Not really, because they use git.
Xiao Zhang pull ed down Xiao Ming's code:
git clone Xiao Ming's git address
Then Xiao Zhang commit ted the code several times
Xiao Zhang uploads the clone code to his remote library
git push -u origin master
Scene: Xiao Zhang merge s his code into Xiao Ming's code base
it seems that the operation of this step cannot be solved through the command of git. You need to log in to Xiao Zhang's remote operation first, click the merge request button on the front page, select the source branch and target branch, and then confirm to establish the merge request.
Scene: Xiao Zhang downloads Xiao Ming's latest submission to the local
# Pull the master branch of the remote host origin and merge it with the local brandest branch. git pull origin master:brantest