This is my second article about getting started

Writing in the front

In our work, most of the code we wrote was version-controlled through Git. Complex Git commands and unfriendly clients may not be very friendly to beginners, and both commands and Git bash are not easy to use.

IDEA as a development artifact, of course, is also integrated into git, this article is for IDEA to use Git as a explanation, even beginners can clearly learn.

Click to get IDEA

Git Basic Operations

First of all, when we join a company, the boss will usually give you a Gitlab link and ask you to pull the code. We will use Git pull, and then we will use Git add and other operations when writing the code after we are familiar with it.

git add

When you create a new class or file, IDEA automatically displays the following prompt, which is similar to git add.

1.

But if you don’t click or skip, you’ll see the new file is red.

2.

You can right-click the Git Add operation and it will turn green

3.

(4)

git commit

After the add file, we need to commit the file. The difference between Git and SVN is that git commits code to a local repository, not to a remote branch.

1.

Right click Commit

2.

Fill in the submission record. Generally, new features start with feat and bug fixes start with fix.

You can see that the file color turns black after submission.

3.

Open the Git console with Alt +9 or View to see the latest commit record.

git push

After committing the code above, we need to push the local code to the remote branch.

1.

After push, we can go to Gitlab to check the records.

2.

git pull

Get the latest version remotely and merge it locally. Right-click on the main project to do Git pull.

1.

Or at the top, depending on personal preference.

2.

git fetch

Fetching the latest version from remote to local does not automatically merge and pulls commits from all branches of the current project.

1.

After using Git fetch, you can see the latest commits from other people, and from the two Master branches and Feat_branch branches, while our local code is unchanged.

We double-click on the commit records of different branches to see the corresponding differences.

2.

git merge

Git fetch does not merge.

1.

2.

Select the corresponding branch.

3.

Through the above operations, you can understand the relationship between Git pull, fetch, merge.

Git fetch is to pull the latest content from the remote host to the local, which the user checks and decides whether to merge into the working native branch.

Git pull is used to pull down the latest contents of the remote host and merge them directly, that is, git pull = git fetch + git merge. Conflicts may occur and need to be resolved manually.

git rebase

Rebase, which is officially dry, is simply used to merge changes from another branch into the current branch.

The dev branch is a new branch that I pulled based on the Master branch.

There have been four commits from the master branch before, and both the Master and dev branches are on the C4 node.

Next, we generate two commits on the dev branch and two commits on the master branch.

At this point, the two branches are in a forked state.

We’re in the master branch, rebase the dev branch

1.

2.

During rebase, a conflict may occur, in which case Git will stop rebase and let you resolve the conflict, and after resolving the conflict, push it to the remote repository.

3.

(4)

At this point we can also see on the console that the master branch has become a straight line.

namely

Of course, in real development, rebase should be used as little as possible, because it will flush out someone else’s commit record if there is a problem with their code, but the latest commit record is yours, you know.

git revert

In multiplayer development, we sometimes get commit errors, and we want to undo the commit and get the application back to where it was before the commit.

Revert rolls a COMMIT back and forth, but keeps previous or later versions. For example, we commit 3 versions C1,C2,C3,

You can use git revert to delete C2 without having to undo the C2 commit. You can use git revert to delete C2 without having to undo the C2 commit.

1.

Commit3 time

2.

Right-click Revert in C2

3.

Merge the C1,C3 version commit

(4)

logging

git reset

Rollback to all commits of a certain version. If you commit 3 versions C1,C2, or C3, use reset to rollback to C1.

Reset changes the position of the HEAD, that is, the position pointed to by the HEAD to a previous version.

HEAD: Every commit with Git, Git automatically strings it into a timeline. This timeline is a branch. If there is no new branch, then there is only one timeline, i.e. only one branch. In Git, this branch is called the master branch. There is a HEAD pointer to the current branch (if there is only one branch, it points to the master, which points to the latest commit). Each version has its own version information, such as unique version number, version name, and so on. As shown below, suppose there is only one branch:

1.

Submit records three times

2.

3.

(4)

The code becomes version C1, but the remote repository code is not rolled back at this point.

5.

The remote repository can be pushed again or re-pulled if it needs to be modified.

Common usage scenarios

There are many git operations mentioned above, in fact, most of the students know, but in the not ideal situation, there are still many problems, such as modify commit record, multi-branch merge, code conflicts and so on.

Git rebase modifies the commit record

If the code is committed too fast, the last commit information is still used, or the description is wrong, but the commit is again (commit only without push).

1.

It is not right to look at the commit record at this point

2.

3.

Modify by rebase operation, select the corresponding submission record, and click Reword

(4)

Revise after clicking Start reps

As you can see, the COMMIT information is now modified and pushed to the remote repository

5.

The above operations are for commit operations, not commit&push operations, which means that local commit records are modified.

Why is this the case?

Commit information is not in the specified format, you commit&push, only commit to local, not remote. For example, in my previous company, the format of the commit message was:

Bugid: XXX or featid: XXXCopy the code

The colon above is in English, but MY hand speed is too fast, so I wrote a Chinese colon, so I can’t push it, so I need to modify it and push it again.

6.

Of course, the easiest way is to right-click the commit record and pop up a change box.

All landowners

Click OK to modify

So somebody said, well, what if the code checks don’t pass? I need to modify the code to commit again, so I have the same function, because the code check is committed twice, the log looks redundant. Look at the following 👇

Git rebase merges multiple commit records

Because the commit is repeated many times, the code functions the same, but because of minor issues, such as the code checking out directly using magic numbers

You need to merge multiple commit records into one, or an initial commit record.

1.

Suppose there are three commit records (no push)

2.

Use rebase to select commit three times and click Squash

3.

(4)

When you look at the commit record, you see that it’s a commit record, and then you push the code

Note: Do not rebase someone else’s code

Cherry-pick obtains the specified commit

In the case of multi-branch code versions, it is common to see code from one branch moved to another. It is common to have two versions, one dev and one master (or release). As a developer, you must develop new features on the dev branch and test them before merging them into the Master branch. This involves how to merge them, because other people may have submitted code during this period. If merge commits all code changes to a branch, then I merge my own code.

1.

Let’s say I developed a feature in Dev, and I’m on the dev branch, and I can see it in the lower corner of IDEA

2.

After the code has passed the test, it needs to go live, at which point it needs to update the code to a fixed version, namely dev– >master

First we can switch to the Master branch on IDEA

3.

At this point, you can see that the master branch also has a COMMIT record, and the code is coming, and then push it again

Git Stash temporarily saves and restores changes

When you are developing a project on the Dev branch and there is a bug in the project that needs to be fixed urgently, you can use the git Stash command to save the modified content to the stack area and then switch to the Hotfix branch to fix the bug. Cut back again to the dev branch and UnStash what you just saved from the stack.

In addition, in the process of switching between branches, the content that should have been developed in the dev branch is developed in the master branch, so it needs to be switched back to the dev branch for development. You can use git Stash to save the content to the stack. After switching back to the Dev branch, the content can be restored again.

1.

If I now add a few classes or modify a file for a new feature, but instead of working in the dev branch, I end up working in the Master branch, and an error occurs when I try to switch back to the dev branch.

2.

So all we need is stash to save our local changes.

3.

Then switch to the Dev branch and click on UnStash

(4)

Applications can be

Stash can also be used to solve the conflict, in general, the first thing at work every day pull code, but sometimes forget, start developing directly, but there are others commit code, that is equivalent to your local code is behind the version, when you write code to pull found there may be a conflict, if files too much, Change is also not possible to go to one CTRL + Z, so you can temporarily save local, keep the code clean, in pull, and then reply.

conclusion

Git GUI can be used by TortoiseGit, SourceTree, GitHub for Desktop, etc. Practice makes perfect.

reference

  • How do I use Git in my work
  • Git has two methods for restoring previous versions: reset and Revert

extends

  • Learn Git, SVN can not forget, tell the difference between them?
  • Git reset differs from revert. Merge differs from cherry pick.

Launch: Get both, use Git in IDEA!