preface

Git is commonly used in most programmers work distributed version control system, in order to solve the problem of the collaboration, several revisions, while Git can be applied to all walks of life, but in fact the programmer to use most, it should be said that one of the essential skills of the programmer, if you still don’t Git, hurriedly to learn, or is likely to be out

It is possible that many students have only used Git on a daily basis and practiced Git commands themselves, but have not actually used Git in their work, that is, they may not have encountered various scenarios where they need to roll back code and resolve conflicts

Git commits to a local repository (stage), commits to a local repository (push), and commits to a remote repository (stage)

This article Outlines the these * * 】 【 submit code several ways to cancel the add operation undo the commit operation 】 【 】 【 revocation push operation 】 【 pull new branch 】 】 【 conflict resolution * *, mainly based on the command line is given priority to, the command line can help you more easy to understand, so after the visualization tools will be more convenient operation, Such as sourceTree

Most smoothly submitted

Start with a smooth flow of requirements development and code submission

Command line

Git checkout master (git checkout master) git checkout master (git checkout master)

Create a local branch: git checkout -b new-branch

Git push origin new-branch commit to remote

Git pull: lets do it if you pull the remote branch code

Git branch — set-upgrade-to =origin/new-branch new-branch

Git pull again, success

Let’s say that everything is going well, and that there are no conflicts with the code you submitted from your colleagues. You might wonder, why is there a conflict in the branch THAT I created separately, and what should I do about it

OK, off work, first hand in the code

You think I’m done with it? How is it possible, how can it be done in one day, when you have a requirement that it’s not easy to design and develop here, it’s not easy to design and develop there

In short, it can’t be developed in a day (smirk

But I want to hand in the code first, I’m afraid someone will steal my computer and my work after work

It takes three steps to put code in a local repository compared to three steps to put an elephant in the fridge

Step 1: use the git add command to place the modified files in the staging area

Git add file or git add

Step 2: Commit to the repository with git commit

Git commit -m ‘commit info

So I can submit my code to a local repository, so that someone steals my computer and my code doesn’t get lost

Not yet, it is only temporarily submitted to the local repository, not yet submitted to the remote, the computer lost the code will still be lost

Let’s do the final step:

Git pull: Pull remote code. This step is used to prevent code conflicts. If the branch is only you, this step can be omitted because there will be no conflicts

But if more than one person is working on the same branch, the code they hand in can conflict

Git server can’t decide which version of code should be kept, so it needs manual intervention

Conflict problem may be more headache for some beginners, the article carries out separately below

Git push Origin new-branch commits local repository code to remote

OK, knock it off!

SourceTree type

Check new branch

Follow the following steps, directly submit, very simple, fool-proof operation

Visualization tools are useful, but it’s a matter of personal habit

Submit with lots of questions

Of course, this is the case where everything goes well, but in real development, there are always a few things that go wrong

Step by step analysis of each step of the problem, how to solve

To cancel the local modification operation

Git status Displays the current file status

Git checkout — xxx. Java undo local changes to a single file. Git checkout — xx. Java undo local changes to a single file

Git checkout — xxx. Java — git checkout — xxx. Java

Git checkout branch-name git checkout branch-name

Undo add to staging (green to red)

Git status Displays the current file status

After the git add operation, it is added to the local staging area and becomes green

Undo green: Undo the ADD operation to the staging area

Git reset HEAD xxx. Java git reset HEAD xxx. Java

After executing the add operation, we found that the original green file (the added file) changed back to the red file. At this point, we undid the add operation

Undo Commit to the local library

After the add and commit operations are performed, the local repository is already committed at this point

Git reset –hard HEAD resets the local file directly to the last committed version, regardless of whether the add and commit operations were performed

Git reset [–mixed –soft –hard] commitID git reset [–mixed –soft –hard] commitID

— Mixed means to undo git add and git commit and keep the editor code.

–soft: revoke git commit, do not undo git add, and keep the editor code.

The –hard parameter is a very violent one. If you decide to use the –hard parameter, you should have decided to discard all the code you changed

** Undo push to remote **

When we perform the push operation, it means that the code has been pushed to the remote. In fact, in this case, there is less need to roll back. Generally, the submitted code does not need to be rolled back again

Of course, Git also considers this aspect, git added the concept of version number to solve this problem, that is, every push will generate a corresponding version number, we can use the version number to carry out the corresponding rollback

View historical commit records in git log

Keep the target version number, that is, the version number you want to return. If the Git log cannot display Chinese characters, run set LESSCHARSET=utf-8

Local code to withdraw to the target version number, git reset – 547 c78001fd2e0eedf83382ef4ee2974e9f445d1 hard, as the local code switch back to the corresponding code version number

Git push –force commits to override remote code. Git push –force commits to override remote code, and the corresponding code is rolled back

Submit carefully! The reset command will reset all submissions after the version number you submitted. Use caution.

Git Revert is another form of rollback called Git Revert. Git revert is a very different form of rollback than Git reset

The difference between reset and revert

Let me give you an example, just to make it easier to understand

Git reset 1 removes all current versions from the list. For example, git reset 1 removes all current versions from the list. Git reset 1 removes all current versions from the list. A new version 4 will be generated with 2 and 3 code, and then version 4 will be committed, so the principle is different

Reset is to return to a Commit. This Commit and previous commits are retained, but any subsequent changes are returned to the staging area

Revert undoes a Commit by creating a new Commit. All previous commits are kept

Reset changes the position of the HEAD to a previous version, as shown in the following figure. Suppose we want to go back to version 1

Application scenario: This method can be used if you want to revert to a previously committed version and we don’t want any later versions

Git Revert is used to reverse a version with the intent of undoing changes made to that version. ** If you want to undo version 2 without having to commit version 3, you can use the git revert command to undo version 2 and create version 4, which retains version 3 and undoes version 2

Usage scenario: If we want to undo a previous version, but we want to keep the later version of the target version and record the change of the version, we can use this method

Use caution when using these two commands, especially the rollback version of reset, because Git will delete later versions of reset

In short, roll back versions with caution

Pull new branches

Command line

Git checkout master (git checkout master) git checkout master (git checkout master)

Create a local branch: git checkout -b new-branch

Git push origin new-branch commit to remote

Git pull: lets do it if you pull the remote branch code

Git branch — set-upgrade-to =origin/new-branch new-branch

Git pull again, success

* * * * sourceTress type

** Resolve conflicts **

Conflict resolution is inevitable in work, work means cooperation, cooperation means that you change code in the same branch, even in different branches of code, and eventually the code you change will converge to the corresponding online branch, and there may be conflicts when you merge to the online branch

For example, if you want to merge two branches dev and master in idea, you need to merge dev to master, switch to dev branch, and perform Git pull to update the local code to the latest

Git merge –no — ff dev

If there is a conflict, you can solve it directly through IDEA. There will be a conflict resolution button. Click “Proceed” to see which files have conflicts

Then click on each conflicting file, select the one you want to keep, and click Apply in the lower right corner to apply

Keep in mind that your idea is in mergeing state, and when you have resolved all the corresponding conflicts, you can commit according to the normal code modification, namely the add Commit push operation mentioned above

In fact, there are many tools or ways to resolve conflicts, everyone has their own code comparison tool to resolve conflicts, when using command line or sourceTree to merge the conflict, I use idea git tool to resolve conflicts most. You can use your own code comparison tool to resolve conflicts and merge code

O praise

Well, that’s all. I’m your captain and your learning partner forever. If you feel this article is helpful to you, click follow me

I hope to be able to make a living by writing articles one day, and I am still honing my skills. This time may take many years. Thank you for being my original reader and communicator. Please believe that as long as you give me a love, I will eventually return you a page of love.

Thank you again for reading here, I will continue to update the technical articles and some records of the soul of life articles, if you feel good, think [Captain] something, please like, follow, share three

Oh, right! The subsequent update of the article I will be put here in time, welcome to click to watch, are dry articles ah, suggest collection, at any time to check

Github.com/DayuMM2021/…

Recommended reading

● The interviewer asked me: Are you sure that using BigDecimal is accurate?

● This GitHub address smells good

● Getting started with message queues

● What is RocketMQ