Basic commands

It is not best to put all changes in the Master branch. The recommended approach is to put changes in branches.


At least one feature branch should be prepared to isolate the changes and merge into the master branch when all features are stable.

When it comes to branches, use the Git branch command.

List all branches:

Git branch lists all local branches.

Git branch -a lists all local and remote branches.

git branch
git branch -aCopy the code

The green ones are local branches and the red ones are remote branches.

The asterisk indicates which branch is currently active at this time.

Create a branch:

Git Branch

git branch mynewbranchCopy the code

Switch branches.

To switch branches, use git checkout

git checkout mynewbranchCopy the code

Then look at the historical record:


Since I haven’t made any changes yet, these branches all point to the same COMMIT, which is just a label/pointer.

Change the branch name.

Git Checkout Master

Then use git branch -m to rename the branch.



Delete branches.

Note that currently active branches cannot be deleted.

Run the git branch -d command to name the branch.

git branch -d newbranchCopy the code

Fast Forward to merge

Quick actions: Switch and create branches:

Git checkout -b

git checkout -b some-changeCopy the code

Then I open a file (index.html) to change the title.



The title of index. HTML was changed in the some-change branch.

If I want to merge this commit into the master branch.

First we switch back to the master branch:

git checkout masterCopy the code

Then, I need to know what has changed, which is to compare the two branches:

git diff master some-changeCopy the code

It can also be viewed visually:

git difftool master some-changeCopy the code

P4merge is not configured on my computer, so the default is probably to use the Vimdiff visualization tool:


Then press Esc and then q to exit.

Finally, there is the merge change: Git merge the branch name that needs to be merged.

git merge some-changeCopy the code

You can see the commits listed, that this is a fast-forward merge, the files involved, and the changes.

Because this is a Fast Forward merge, Git put all commits in the master branch as though they were unchanged:


But a fast-forward merge can only be performed if this condition is met: the master branch does not change at the time of the merge.

The status after the merge is as follows:


After merging, this branch is no longer needed, delete:

git branch -d some-changeCopy the code

At this point, the Git log only contains the master.


The Fast Forward merger is disabled

Add a branch and switch to that branch:

git checkout -b add-textCopy the code

Then I add some text to index. HTML and commit.

Then modify readme. md to add the text commit.

Now on the add-text branch, there are two commits. Look at the log:


And then merge that branch into the master branch.

Git merge first switch to the master branch and then git merge, but this time, I want to leave a trace of my branch’s process, so I want to disable the fast-forward merge:

Git merge Git merge branch –no-ff

To do so, a new merge COMMIT will appear,


Then check git log:


As you can see, the whole branching process is recorded.

Finally, delete the branch and view log:


As you can see, after deleting the branch, the name of the branch is gone from the log, but the branch is still there.

automerge


Create a branch and switch:

git checkout -b simple-changesCopy the code

Modify a file and commit:


Then back to the master branch:

Modify another file, and commit:


Look at the log:


The inside of the red line is interesting, the two branches have changed /commits.

The situation can be combined like this:

Git merge -m “custom info”

git merge simple-changes -m "merging changes from simple-changes"Copy the code

Look at the log:


You can see that the merge of the branches is complete.

Finally, delete the branch and view log:


The effect is the same, the branch name is gone, but the history of the branch remains.

You can open up both files and look at them, and the changes are still there.

Resolve merge conflicts


If changes are made to the same file on both branches, conflicts can occur.

First create a branch and switch to it:


Then modify the index.html, in a few places.

Then look at the status and commit:


Then switch to Master and edit the same file:


The index. HTML is not what the realWork branch looks like after modification, but what it looked like before:


Then modify the index.html to fix a few possible conflicts.

commit:


Then look at log:


The next best thing to do is diff:


Diff can also be performed using visual tools:


Let’s merge:


As expected, a conflict occurred and the automatic merge failed.

The current state should be called the Merging state.

Now open index.html like this:


You can see two conflicts, each with the HEAD(Master) version and the RealWork branch.

In the figure above, I use Visual Studio Code. I can either click the above button to resolve the conflict or manually modify the file to resolve the conflict.

But here I use my configured MergeTool:

Git mergetool. This command will open p4merge(my locally configured mergetool):


Click the icon to select a different version, and finally click Save.

Close the p4merge:


Then commit:


After the commit, the state is no longer complex, but an untraced file appears:


This is because Git saves a raw, tender version for use when resolving conflicts:


Orig files are not supposed to be tracked, however, so they need to be added to the.gitignore file:


Then check the status:


This time only.gitignore has changed.

commit:


Finally, you can delete the RealWork branch:


Look at the log:


Ok.