A list of commonly used git commands (with detailed explanation)

1. Create a warehouse

To use the current directory as a Git repository, we just need to initialize it.

git init

After the command is executed, a .git directory will be generated in the current directory.
Use our designated directory as the Git repository.

git init newrepo

After initialization, a directory named .git will appear under the newrepo directory. All data and resources needed by Git are stored in this directory.

If there are several files in the current directory that you want to include in version control, you need to use the git add command to tell Git to start tracking these files, and then submit:

git add *.c
git add README
git commit -m '初始化项目版本'

The above command submits the directory ending with .c and the README file to the warehouse.

2. Check out the warehouse

We use git clone to copy the project from the existing Git repository (similar to svn checkout).

The command format for cloning a warehouse is:

git clone <repo>

If we need to clone to a specified directory, we can use the following command format:

git clone <repo> <directory>

Parameter Description:

repo: Git repository.
directory: local directory.

For example, to clone the Git code repository Grit of the Ruby language, you can use the following command:

git clone git://github.com/schacon/grit.git

After executing this command, a directory named grit will be created in the current directory, which contains a .git directory to save all the downloaded version records.

If you want to define the name of the new project directory yourself, you can specify the new name at the end of the above command:

git clone git://github.com/schacon/grit.git mygrit

Execute the following command to create a cloned version of the local warehouse:

git clone /path/to/repository 

If it is a warehouse on a remote server, your command will look like this:

git clone username@host:/path/to/repository

3. Workflow

Your local repository consists of three "trees" maintained by git. The first one is your working directory , which holds the actual file; the second is the temporary storage area (Index) , it is like a cache area, temporarily save your changes; and finally the HEAD which points to the results of your last submission .

You can propose changes (add them to the staging area), using the following command:

git add <filename>
git add .

This is the first step in the basic git workflow; use the following command to actually commit the changes:

git commit -m "代码提交信息"

Now, your changes have been submitted to HEAD , but they have not yet reached your remote repository.
Workflow

4. Push changes

Your changes are now in the HEAD of the local repository. Execute the following commands to submit these changes to the remote repository :

git push origin master

You can replace master with any branch you want to push .

If you have not cloned an existing warehouse and want to connect your warehouse to a remote server , you can add it with the following command:

git remote add origin <server>

So you can push your changes to the added server.

5. Branch

Branches are used to insulate feature development. When you create a repository, master is the "default" branch . Develop on other branches and merge them into the main branch after completion.
List basic branch commands:

git branch

Without parameters, git branch will list your local branches.

$ git branch
* master

The meaning of this example is that we have a branch called master, and that branch is the current branch.
When you execute git init, Git will create the master branch for you by default.
The branch is completed locally and is fast. To create a new branch, we use the branch command.

git branch test

The branch command does not bring us into the branch, it just creates a new branch. So we use the checkout command to change the branch.

git checkout test

Create a branch called "test" and switch to it:

git checkout -b test

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

git checkout master
git merge test

If you want to delete a branch, we use the -d flag. Delete the newly created branch:

git branch -d test

Unless you push the branch to a remote repository, the branch is invisible to others:

git push origin <branch>

Branch

6, update and merge

To update your local repository to the latest reform move, execute:

git pull

To fetch and merge remote changes in your working directory.
To merge other branches into your current branch (for example, master), execute:

 git merge <branch>

In both cases, git will try to merge changes automatically. Unfortunately, this may not be successful every time, and conflicts may occur. At this time, you need to modify these files to manually merge these conflicts (conflicts). After the modification, you need to execute the following command to mark them as merged successfully:

git add <filename>

Before merging the changes, you can preview the differences with the following command:

git diff <source_branch> <target_branch>

7. Label

Creating tags for software releases is recommended. This concept already exists, and it also exists in SVN. You can execute the following command to create a label called 1.0.0:

git tag 1.0.0 1b2e1d63ff

1b2e1d63ff is the first 10 characters of the commit ID you want to mark. You can use the following command to get the submission ID:

git log

You can also use fewer first few digits of the submission ID, as long as its pointing is unique.

8. Replace local changes

If you make a mistake (of course, it's best to never happen), you can replace the local changes with the following command:

git checkout -- <filename>

This command will replace the files in your working directory with the latest content in HEAD . Changes that have been added to the staging area and new files will not be affected.

If you want to discard all your local changes and commits, you can get the latest version history on the server and point your local master branch to it:

git fetch origin
git reset --hard origin/master

9. Git remote warehouse (Github)

To add a new remote warehouse, you can specify a simple name for future reference. The command format is as follows:

git remote add [shortname] [url]

View the current remote library. To view which remote warehouses are currently configured, you can use the command:

git remote

Instance

$ git remote
origin
$ git remote -v
origin    [email protected]:tianqixin/runoob-git-test.git (fetch)
origin    [email protected]:tianqixin/runoob-git-test.git (push)

Add the -v parameter when executing, you can also see the actual link address of each alias.
Git has two commands to extract updates from remote repositories.
1. Download the new branch and data from the remote warehouse:

git fetch

After the command is executed, you need to execute git merge to remote branch to your branch .
2. Extract data from the remote warehouse and try to merge to the current branch:

git merge

This command is to execute git fetch immediately after executing git merge remote branch to any branch you are in.
Suppose you have configured a remote warehouse, and you want to extract updated data, you can first execute git fetch [alias] to tell Git to get the data that you don’t have, and then you can execute git merge [alias]/[branch ] To merge any updates on the server (assuming someone pushed to the server at this time) to your current branch.
Push your new branch and data to a remote warehouse command:

git push [alias] [branch]
git push origin master

To delete a remote repository, you can use the command:

git remote rm [别名]

Instance

$ git remote -v
origin    [email protected]:tianqixin/runoob-git-test.git (fetch)
origin    [email protected]:tianqixin/runoob-git-test.git (push)

# 添加仓库 origin2
$ git remote add origin2 [email protected]:tianqixin/runoob-git-test.git

$ git remote -v
origin    [email protected]:tianqixin/runoob-git-test.git (fetch)
origin    [email protected]:tianqixin/runoob-git-test.git (push)
origin2    [email protected]:tianqixin/runoob-git-test.git (fetch)
origin2    [email protected]:tianqixin/runoob-git-test.git (push)

# 删除仓库 origin2
$ git remote rm origin2
$ git remote -v
origin    [email protected]:tianqixin/runoob-git-test.git (fetch)
origin    [email protected]:tianqixin/runoob-git-test.git (push)

10. Practical tips

Built-in graphical git:

gitk

Colored git output:

git config color.ui true

When displaying history, only one line is displayed for each submitted information:

git config format.pretty oneline

Interactively add files to the staging area:

git add -i

Guess you like

Origin blog.csdn.net/Serena_tz/article/details/113977462