Git five-minute tutorial


发布时间 : 2022-11-18 07:07:06 UTC

Page Views: Stats unavailable

Introduction

git

Before using Git, you need to set up a warehouse (repository). You can use an existing directory as a Git repository or create an empty directory. Using your current directory as the Git repository, we just need to initialize it.

<pre class="brush:py;"> Git init </pre>

Use the directory we specified as the Git repository.

<pre class="brush:py;"> Git init newrepo </pre>

From now on, we will assume that you are in the root directory of the Git repository, unless otherwise noted.

Add a new file

We have a warehouse, and we can use the add command to add files.

<pre class="brush:py;"> Git add filename </pre>

You can use add... continue to add task files.

Submit version

Now that we have added these files, we want them to be actually stored in the Git repository. To do this, we submit them to the warehouse.

<pre class="brush:py;"> Git commit-m "Adding files" </pre>

If you do not use-m, an editor will appear to allow you to write your own comments. When we modify a lot of files and don't want every one of them to add, if you want commit to automatically submit local changes, we can use the-a flag.

<pre class="brush:py;"> Git commit-a-m "Changed some files" </pre>

The-an option of the git commit command submits all documents that have been modified or deleted and have been managed by git to the repository. Please note that-a will not cause new documents to be submitted, but can only be modified.

Release version

Let's clone a library from the server and upload it.

<pre class="brush:py;"> Git clone ssh://example.com/~/www/project.git </pre>

Now we can push it to the server after we modify it.

<pre class="brush:py;"> Git push ssh://example.com/~/www/project.git </pre>

Retrieve updates

If you have push as above, the following command indicates the current branch is automatically merged with the only tracking branch.

<pre class="brush:py;"> Git pull </pre>

Updates from a non-default location to the specified url.

<pre class="brush:py;"> Git pull http://git.example.com/project.git </pre>

It's been more than five minutes?

Delete

If you want to delete files from the repository, we use rm.

<pre class="brush:py;"> Git rm file </pre>

Branching and merging

The branch is completed locally and is fast. To create a new branch, we use the branch command.

<pre class="brush:py;"> Git branch test </pre>

The branch command doesn't take us to the branch, it just creates a new branch. So we use the checkout command to change the branch.

<pre class="brush:py;"> Git checkout test </pre>

The first branch, or main branch, which is called "master".

<pre class="brush:py;"> Git checkout master </pre>

Changes to other branches are not reflected on the primary branch. If you want to commit changes to the main branch, you need to switch back to the master branch and then use merge.

<pre class="brush:py;"> Git checkout master Git merge test </pre>

If you want to delete the branch, we use the-d flag.

<pre class="brush:py;"> Git branch-d test </pre>