Basic operations of Git - command line

Git workflow

The details are as follows:

Local warehouse: is the Git warehouse on the developer's own computer, which stores our code (the .git hidden folder is our local warehouse)

Remote warehouse: is a Git warehouse on a remote server to store code (it can be a warehouse on github.com or gitee.com, or your own company's server)< /span>

Workspace: Where we write our own code (documents)

Staging area: A special file (index) in the local warehouse is called the staging area, which temporarily stores the file area we are about to submit.


clone:Clone the code from the remote repository to the local repository.

checkout:Check out a warehouse branch from the local warehouse and make revisions.

add (add):Submit the code to the staging area before submitting.

commit: is submitted to the local warehouse. Each modified historical version is saved in the local warehouse.

fetch (fetch): Fetch from the remote warehouse to the local warehouse without any merging action. Generally, there are relatively few operations.

pull (pull): Download the code from the remote warehouse to the local warehouse, automatically merge it (merge), and then put it into the workspace, which is equivalent to fetch+merge.

push:After the modification is completed and the code needs to be shared with the team, push the local warehouse code to the remote warehouse.

1. Local warehouse operations

1. Configure the environment

The first thing you need to do when installing Git is to set up your username and email address. This is very important because every Git commit uses this user information.

# Set user information
git config --global user.name "user name"
git config --global user.email "email address"
# View configuration information
git config --global user.name (View user name configuration information)
git config --global user.email (View email address configuration information)
git config --list (view all git configuration information)

2. Initialize the local warehouse init

# Initialize warehouse with work area
git init

3. Add the workspace to the temporary storage area add

git add <file name> or git add . (Add all files in the current directory to the staging area)

4. Submit the temporary storage area to the local warehouse commit

git commit -m "comment content" <file name>  

5. Check the modification status status

git status

6. View log submission records

git log
git reflog (deleted records also exist)

7. Version rollback/switching

git reset --hard commitID (commitID can be viewed using the git log command)

8. Clone clone

# Clone from remote repository
git clone remote Git warehouse address 
For example: git clone https://gitee.com/harrietmao/git_studyfirst.git

2. Remote warehouse operations

Remote name: Default is origin, depends on remote server settings

1. View remote

# View remote list the abbreviation of each specified remote server
git remote 
# View the remote, list the short name and address
git remote -v  
# View the detailed address of the remote warehouse
git remote show <repository name>

2. Add/remove remote warehouse

# Add remote warehouse
git remote add <remote name> <warehouse address url>
Remote name: The default is origin, depending on the remote server settings
Warehouse address: Get this address URL from the remote server
# Remove the relationship between the remote warehouse and the local warehouse (it only removes the relationship between the remote warehouse locally and does not really affect the remote warehouse)
git remote rm <shortname> 

3. Push to the remote warehouse push

git push [remote-name] [branch-name]
If the remote branch and the local branch have the same name, you can write only the local branch
git push origin master == git push origin master:master
**--set-upstream pushes to the remote end and establishes an association with the remote branch
git push --set-upstream origin master
    --If the branch is already associated with the remote branch, the branch name and remote name can be omitted
    git push pushes the master branch to the associated remote branch. 

4. View the relationship between local branches and remote branches

git branch -vv

5. Clone, fetch, merge, and pull from the remote warehouse (pull=fetch+merge)

# Clone from remote repository
git clone <url> 
# Fetch from the remote warehouse (pull to the .git directory, will not be merged into the workspace, and the workspace will change)
git fetch <shortname> <branch name>
# Manual merge Merge a branch of a certain version into the current workspace
git merge <shortname>/<branch name>
# Pull from the remote warehouse (pull to the .git directory, merge into the workspace, the workspace will not change) = fetch+merge
git pull <shortname> <branch name>
git pull <shortname> <branch name> --allow-unrelated-histories # Force pull merge

Note: If the current local warehouse is not cloned from the remote warehouse, but a locally created warehouse, and files exist in the warehouse, an error will be reported when pulling files from the remote warehouse (fatal: refusing to merge unrelated histories). To solve this problem To solve the problem, you can add the parameter --allow-unrelated-histories after the git pull command (command above)

# Push the local warehouse to a branch of the remote warehouse
git push [remote-name] [branch-name]

3. Branch

#The default branch name is master

1. View branches

# List all local branches
git branch
# List all remote branches
git branch -r
# List all local branches and remote branches
git branch -a

2. Create a branch

git branch branch name

3. Switch branches (checkout)

git checkout branch name
git checkout -b branch name (switch to a non-existent branch--create and switch)

4. Delete branch

The current branch cannot be deleted, only other branches can be deleted

# Delete branch (if the branch has been modified, deletion is not allowed)
git branch -d <branch name> (Before deleting the branch, various checks need to be done)
#Force deletion of branch
git branch -D <branch name> (no checking, forced deletion)
# Delete remote warehouse branch
git push origin –d <branchName>

5. Submit the branch to the remote warehouse

git push <repository name> <branch name>

6. Merge branches and merge other branches into the current workspace

git merge <branch name>

Guess you like

Origin blog.csdn.net/weixin_61275790/article/details/134222078