The article is from my blog

The text before

The last article briefly introduced Git and GitHub, and this article uses a repository as an example to introduce basic Git usage rather than simply listing every Git command

The body of the

1. Initial operation of the warehouse

Initialize the

First of all, I will create a folder named “git-test” in the local GitHub repository folder.

For now, this is just a plain empty folder, so start Git Bash inside it:

To turn an empty folder into a GitHub repository, first initialize the repository with Git:

git init

Although the folder does not appear to have changed after the repository is initialized, there is a.git directory inside the folder, which is hidden by default and can be seen when the hidden file is opened:

View warehouse Status

Check the status of this repository:

git status

Add files

I’ll start by creating a readme.md in the repository with a single word “test”

Then look at the warehouse status and you’ll see the change:

Files that are uncommitted are classed as Untracked files

The commit process is a two-step process, adding the file to the staging area, and then committing

First we commit the readme. md file to the staging area:

git add README.md

If you want to add all changed files to the staging area, use:

git add .

Then we check the warehouse status:

Files in Changes to Be Committed are files that have been temporarily saved but not committed

Next we submit the file:

git commit -m 'Update README.md'

-m is followed by the information for this submission, which is equivalent to an overview

The readme. md was already in my repository before it was submitted, so why submit it?

Because the ultimate goal of the repository is to open to the outside world, if you do not submit or push your changes to the local repository, there will be no changes in the remote repository on GitHub, and other people will not see your changes. As for submitting changes to the remote repository, I will explain below

See the log

In multi-person collaboration, it is often necessary to look at the log to see who committed:

Git log will see that I just committed the readme.md file

2. Branch of warehouse

The next branch is an important concept that is used in collaboration with many people

First, when we initialize the repository, there is a default master branch. If no other branches are created, all work is done on the master branch. If we want to create branches:

git checkout -b newBranch

You’ll notice that you’ve switched to my newBranch, newBranch

-b is followed by the name of the branch. If the branch exists, go to that branch. If it does not, create a new branch

If you want to create a new branch, the above statement can be divided into two steps:

git branch newBranch git checkout newBranch

Create a branch, then switch to a branch

3. Remote warehouse

So far, we have done all our operations on the warehouse locally. Let’s talk about the remote warehouse:

Start by creating a repository on GitHub with the same name as the local repository above:

The git-test repository is a private repository for blogging purposes.

After you have created it, you will be prompted:

This repository will act as a remote repository for the local repository. In short, we will associate our local git-test repository with the GitHub remote git-test repository

Push content to a remote repository

We chose the first option. Initialization, adding readme. md, and committing changes are done. Now we add the remote repository, copy SSH, and use:

git remote add origin [email protected]:lihanxiang/Git.test.git

After execute this command, Git will according to [email protected]: lihanxiang/Git. Test. The Git to set we just created warehouse to local remote warehouse warehouse, name of origin

The name Origin is different from the name of our repository on GitHub. When we push content, the name we fill in is the name we specified when setting up the remote repository

Then push the contents of the local repository to the master branch of the remote repository:

git push -u origin master

Then refresh the repository’s page on GitHub:

At this point we have pushed the content from the local repository to the remote repository

Get content from the remote repository

If I create a new file in the master branch of the remote repository, I need to get the new contents of the remote repository before I operate on the local repository:

git pull origin master

4. To summarize

Git’s basic operations are described above. Of these methods, there are a few that are most commonly used:

git init

git status

git checkout

git add

git commit

git pull

git push

The last four steps are usually linked together

This is just a brief introduction to Git, and the next article will cover branching