preface
The last article on the basic use of GIT and its principle explains the basic operation and principle of GIT, this article mainly explains some centralized development of GIT encountered problems and solutions, no more nonsense, directly into the main topic.
Learn the key
Git Clone remote code
Git Clone remote code process is shown below:
Now let’s simulate the process locally
- Initialize the local remote code repository using the following command:
git init --bare
Copy the code
As an example, create two remote code repositories a and B locally, as shown below:
After executing the above command, the a.emote and B.emote folders will have the following files:
Suppose there are two developers, Alice and Michael, and Alice has created an iOS project DemoApp and initialized Git, as shown below:
Alice starts writing code, as shown below:
Alice finds Michael. Since they are far away from each other, Alice needs to upload the code to the remote code repository, so that Michael can download the code he wrote halfway, and both of them can develop and maintain it together. Therefore, a remote code repository needs to be added using git command, as shown below:
Git remote add -t Specifies which branch of the remote repository to track. Git remote add -t Specifies the branch name of the remote repositoryCopy the code
An example is shown below:
Save the code to Git and commit it
To commit the code from the local repository to the remote, use the following command:
Git push --set-upstream origin git push --set-upstream originCopy the code
An example is shown below:
Now Michael can pull the code that Alice wrote from the remote repository A.emote, as shown in the figure below
Then use the following command to pull the code from the remote repository:
git pull
Copy the code
An example is shown below:
Michael can then start coding happily.
2. How does Git conflict
Now Alice and Michael are coding together. Alice writes a piece of code and submits it to the local code repository and the remote code repository, as shown below:
Alice wrote another piece of code and submitted it to the local code repository and the remote code repository, as shown below:
After two commits, the commit record looks like this:
Git log --oneline -- class --graph --statCopy the code
But Michael doesn’t know that Alice has submitted the code to the remote repository twice, so he continues to write the code locally. First, Michael writes a piece of code in line 21 and submits it to the local repository, as shown below:
Michael then writes another piece of code at line 22 and submits it to the local repository, as shown below:
Michael’s local submission record is as follows:
Michael then pushes the code from the local repository to the remote repository, as shown below:
As you can see from the above error message, a code conflict has occurred. You can use the following command area to resolve the conflict:
Git mergetool --tool-help git mergetool Git config merge. Tool opendiff git mergetoolCopy the code
The conflicting code shown in Xcode is shown below:
Michael feels that his code is not as good as Alice’s, so he deletes Alice’s code and then submits it to the remote repository, as shown below:
The downside of Michael’s approach is that it makes the master branch of the remote repository look bloated, creating many bugs as shown in the figure below that are not easy to understand and maintain.
Michael thinks this is a bad idea, so he falls back to the pre-merge commit with the following command:
Commit git rev-parse --help commit git reset --hard ORIG_HEADCopy the code
An example is shown below:
Look at the commit record of the rollback merge, as shown in the following figure:
3. How to resolve Git conflicts
If Michael wants to resolve the above code conflicts, instead of pulling the code directly, he should do a rebase operation, as shown in the following command:
Git pull Remote repository alias Remote repository branch -- RebaseCopy the code
An example is shown below:
After executing this command, an error is reported because of a code conflict, as shown in the following figure:
The above execution process is to compare Michael’s conflicting COMMIT object B3066AA with HEAD in the remote code repository until the last child node of Michael’s commit object is compared. Therefore, Alice’s code is selected here, as shown below:
Then save the modified code to the git file system and modify the key information of the file to the index file.
git rebase --continue
Copy the code
An example is shown below:
After executing this command, an error is reported because of a code conflict, as shown in the following figure:
Here we compare Michael’s last commit object, 261820E, and find code conflict, so we report an error. Here we still choose to keep Alice’s code, as shown below:
Then save the modified code to the git file system and modify the key information of the file to the index file. Proceed with the rebase operation, as shown in the picture below:
The rebase operation is successful. Check the code submission information as shown in the following figure:
You’ll notice that rebase appends Michael’s commit to the end of the remote branch commit and does not generate the tumor branches that pull and merge generate, making it easier to understand and manage the code.
4. Rebase example
First define the directory structure as shown below and create an iOS project in the Alice folder:
The directory structure for this project is as follows:
Initialize the remote repository in the A.emote folder, as shown below:
Go back to the Alice folder to initialize Git, add the remote code repository A.emote and submit the project project code, as shown below:
Push code from the local repository to the remote repository as shown below:
Assuming that the code has been in development for a long time, Alice is responsible for coding the main framework and the Home module, and has submitted the code several times. Only the first submission is shown in its entirety here, as shown below:
; Pull remote repository code, push local repository code to the master branch of the remote repository
; Alice then commits several more times to the remote repository, committing the log (see git log –oneline — pipeline –graph –stat) as follows:
; In addition to viewing the submission record on the terminal, you can also use GitUp graphical tools to view it (you can click on GitUp to download it on the official website), as shown below:
Now Alice is coding the Home module again, so she creates a branch, Alice_Home, and switches to it, as shown below:
Alice wrote the code in the Home module and submitted it to the remote repository, as shown below:
Next, Alice wrote and submitted code in ice_Home several times, as shown below:
Then, Alice switches to the Master branch, modifies the code of the Home module and submits it, as shown below:
At this point, the situation of each branch is shown in the figure below:
Alice then performs the rebase operation to merge the Alice_Home branch code into the master branch using the following command:
Git rebase git rebase git rebase git rebaseCopy the code
Use the tool to resolve the conflict and submit the code, as shown below:
Save the modified code to the git file system and the key information of the modified file to the index file, then continue rebase, continue rebase, as shown in the picture:
Then we will look at the branch situation, as shown in the figure below:
You can see that the commit object in Alice_Home is now rebase in the master branch of the local repository, so you can remove the Alice_Home branch from the local repository, as shown below:
After deleting, the current branch looks like this (left is local master branch, right is remote master branch) :
However, you also need to rebase the master branch of the local repository to the master branch of the remote repository, as shown in the following figure:
The first rebase operation didn’t cause any conflicts, so git add was executed first. Run the following command to skip rebase:
git rebase --skip
Copy the code
The process is shown in the figure below:
Then a code conflict occurs. After resolving the conflict, execute the git add. command and proceed with rebase, as shown below:
After rebase is complete, commit the code and push the local repository master branch code to the remote repository Master branch as shown below:
Finally, the branching situation is shown in the figure below:
In fact, the above conflicts are displayed in a two-way merge mode. When resolving these conflicts, it is easy to be confused. You can use the following command to configure the three-way merge mode to display the conflict code:
git config merge.conflictstyle diff3
Copy the code
An example is shown below:
If you need to cancel the current rebase operation and go back to the code before rebase, you can use the following command:
git rebase --abort
Copy the code
5. Edit the commit object in the branch
You can also modify, merge, or delete a commit object on a branch. Before you demonstrate this, let’s take a look at the commit object information from the previous project.
To edit a COMMIT object, run the following command:
Git rebase -i commit Object IDCopy the code
Use the following example (edit a commit object with id 8FF1e8f, preferably with the id of the commit object before it) :
- If you want to change it
id
for8ff1e8f
thecommit
Object submission information can be manipulated as follows:
After you save and exit, enter git commit –amend as shown below:
Then edit the submission information, as shown below:
After saving and exiting, you will be prompted with the following information:
Finally, you need to execute git rebase –continue to end the rebase, as shown in the following figure:
Note: Modifying a Commit object actually creates a new commit object (the new commit information) to replace the original commit object
- If you want to compress more than one
commit
Object, which can be operated as follows:
The following editing interface will be displayed:
After the compression is successful, the prompt message is as follows:
View the commit object in the branch as shown below:
- If you want to delete something
commit
Object and those that follow itcommit
Object, which can be operated as follows:
After deletion, the remaining commit objects in the branch look like this:
To be continued…