Git commands that programmers must master

Author: Zen and the Art of Computer Programming

1 Introduction

What is Git?

Git is an open source distributed version control system for agile and efficient handling of any project, small or large. Git encourages a work style of excellence, efficient communication skills, and can adapt to the needs of short-term, medium- and long-term changes; it is also one of the most popular version management tools in the world.

Why learn Git?

Due to the unique characteristics of Git, it can help developers easily achieve the following functions:

  • Provide snapshot backup, record the differences of each submission, resolve conflicts, and effectively prevent file loss or data omission;
  • Intelligent merging, by analyzing file changes and automatically merging them, avoids the trouble of manual merging and reduces the error rate;
  • Branch management allows multiple people to collaborate on development in the same warehouse. Everyone can work on their own branch to avoid interfering with each other;
  • Supported by hosting platforms such as GitHub/Bitbucket, open source projects on various platforms can use Git to achieve version control. Therefore, learning Git has the following advantages:
  • Skill improvement: Learning Git can help you further improve your programming skills. After mastering Git, you can use it to perform advanced operations such as automated builds, version management, and automated deployment;
  • Exercise teamwork spirit: As a distributed version control system, Git allows multiple developers to work together, share code, resolve conflicts, and improve collaboration efficiency;
  • Increased interest: Learning Git can increase your interest in programming, try different development models, and explore different development processes;
  • Social value: Understand the design concept behind Git, and you will find many interesting features, creativity, and practicality;
  • Good compatibility: Git is compatible with various operating systems and hardware, and can run on all large and small projects, as well as in a virtual machine environment.

    2. Explanation of basic concepts and terms

    Git uses a graphical interface, so you need to be familiar with some basic Git commands before starting to learn. These commands are introduced in this section.

    2.1 Git basics

    2.1.1 Commands and workspaces

    Command: A collection of operation instructions provided by Git, which can be used to perform various operations on the Git warehouse by entering commands. Workspace (Working Directory): It is a directory on your computer that stores the files you are editing. Staging area (Stage): The staging area is generally called the "index" and is used by Git to save the list of currently tracked files. Repository: Git is a version control system based on a repository. All version history records are saved in the version repository. The relationship between the staging area and the version library: the staging area is a place where the user has modified but has not yet submitted it to the local warehouse, while the version library is a place where it has been submitted to the local warehouse.

2.1.2 Basic configuration

Set username and email

First, set up your username and email. Run the following command:

git config --global user.name "your name"
git config --global user.email "your email address"

Among them, --globalthe parameter means that it is globally effective, that is, all warehouses of the current user will use this user name and email address by default. If you only want to take effect for the current warehouse, you can omit this parameter.

View configuration information

To view configuration information, use the following command:

git config --list
Modify configuration information

If you need to modify the configuration information, you can use the following command:

git config [--local|--global|--system] section.key value

Among them, sectionrepresents the configuration group, for example user, alias. keyIndicates the name of a specific configuration item, for example name, email. valueIndicates the value of a specific configuration item.

2.1.3 Basic usage

Initialize warehouse

When initializing the warehouse, you need to specify the path to the warehouse. If the warehouse does not exist, an empty warehouse will be created. Run the following command:

git init [path]

For example:

git init /Users/michael/Desktop/test
Add files to cache

After adding a file to the cache, the commit operation will not actually be performed, but the file will only be marked as pending submission. Run the following command:

git add filename(s)

For example:

git add test.txt
View status

View the file status of the current cache area, including tracked files (Tracked), untracked files (Untracked) and files in the staging area (Staged). Run the following command:

git status
Commit changes

Submit the files in the staging area to the repository. Run the following command:

git commit -m "message"

-mParameter means adding commit comments.

Pull remote warehouse

Pull the code from the remote repository to the local repository. Run the following command:

git pull origin master

Among them, originrepresents the name of the remote warehouse and masterrepresents the branch name.

Push local warehouse

Push the contents of the local warehouse to the remote warehouse. Run the following command:

git push origin master

Among them, originrepresents the name of the remote warehouse and masterrepresents the branch name.

2.2 Git branch management

Branch: Git allows you to create your own independent branch, so you can freely try and make mistakes without affecting the code of other developers.

2.2.1 Create a branch

Create a new branch and run the following command:

git branch <branch-name>

For example:

git branch dev

2.2.2 Switch branches

Switch to the specified branch and run the following command:

git checkout <branch-name>

For example:

git checkout master

2.2.3 Delete branch

To delete the specified branch, run the following command:

git branch -d <branch-name>

2.2.4 Merge branches

To merge the specified branch into the current branch, run the following command:

git merge <branch-name>

For example:

git merge dev

2.3 Git remote warehouse management

Remote warehouse: Git supports the creation of remote warehouses to share code between different machines.

2.3.1 Configure remote warehouse

Configure the address of the remote warehouse and run the following command:

git remote add origin git@server-address:/path/to/repository.git

Among them, originit represents the alias of the remote warehouse, which can be customized. git@server-addressIndicates the server address and path/to/repository.gitthe path of the remote warehouse.

2.3.2 Push local branch to remote warehouse

Push the local branch to the remote warehouse and run the following command:

git push -u origin <branch-name>

-uThe parameter indicates that the local branch is associated with the remote branch, and then the remote branch can be referenced by abbreviation.

2.3.3 Pull branches from remote repository

Pull the branch from the remote warehouse to the local one and run the following command:

git fetch origin <branch-name>:<local-branch-name>

Among them, <branch-name>represents the name of the remote branch and <local-branch-name>represents the name of the local branch.

2.3.4 View remote warehouse

To view the information of the remote warehouse, run the following command:

git remote show origin

Guess you like

Origin blog.csdn.net/universsky2015/article/details/133504803