Learn Git version control tool

Most commonly used commands

git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/yinqishuo/TwinBee_Godot.git
git push -u origin main
git checkout .

What is Git

Git is a free and open source distributed version control system that makes team collaboration more efficient. Git can track changes to files and save a history of each version. This allows team members to work better together, view their work, and merge code more easily.

Git has three main areas: work area, staging area and warehouse. Their meanings and relationships are as follows:

  • Workspace: It is the directory you can see on your computer. You can modify, create, delete, etc. files.
  • Staging area: It is a hidden file located in the .git directory. It records the information of the files you add using the git add command, but does not save the file itself, but points to each file through its ID.
  • Warehouse: Also located in the .git directory, it saves all versions of files you submit using the git commit command, as well as branch, tag and other information.

After you modify the files in the workspace, you can use the git add command to add them to the staging area, indicating that the files are ready for submission. Then you can use the git commit command to submit the files in the staging area to the warehouse, indicating that these files have become a new version. If you want to undo the changes in the workspace or staging area, you can use the git checkout or git reset command to achieve this. Of course, we also have the remote warehouse Github. You can use the git push command to upload the warehouse to the cloud.

Insert image description here

Write picture description here

Install Git

Before you start using Git, you need to install Git on your computer. You can download the Git installer for your operating system on the official website .

After the installation is complete, you can run the git command in a command line window (such as Terminal or Git Bash) to check whether Git was installed successfully.

Create a Git repository

To use Git to track the history of a project, you need to create a Git repository. Before creating a Git repository, you need to choose a directory to store your project. Go into your project directory and run the following command to create a Git repository:

git init

This will create a subdirectory named .git in your project directory, which is where Git will keep the metadata and object database for the repository.

Add files to Git repository

Once your Git repository is created, you can add files to the repository to track them. Add the files to the Git repository using the following command:

git add <filename>

This will add the <filename> file to the staging area of the Git repository. After you make changes to a file and run the git add command again, Git records changes to all files.

Commit changes

Once you have added the files you want to track, you can commit the changes to Git local repository using the following command:

git commit -m "提交说明"

-mParameters are used to add commit instructions describing the changes you made.

View the status of your Git repository

You can use the following command to view the status of your Git repository:

git status

This will show all changed files and unstaged files.

View commit history

Use the following command to view the commit history of a Git repository:

git log

This will list the history of all commits, including commit description, committer, and timestamp.

branch management

Git allows you to create and manage multiple branches, which can be used to develop different features or switch between different versions.

Create a new branch using the following command:

git branch <branchname>

This will create a new branch named <branchname>. The commonly used (Github) main branch name is master. Use the -d option to make a branch It is deleted after being pushed and merged into the remote branch. Use the -D option to force a branch to be deleted immediately.

You can switch to this branch using the following command:

git checkout <branchname>

Once you make changes in the new branch, you can merge those changes into the master branch. First, you need to switch back to the master branch:

git checkout main

Then, merge the changes from branch <branchname> into the master branch using the following command:

git merge <branchname>

This will merge the changes from the <branchname> branch into the master branch.

Remote warehouse

Git also allows you to interact with remote repositories so you can share code with other team members.

Use the following command to connect the local repository with the remote repository:

git remote add <remote> <url>

<remote> is the alias of the remote warehouse, usually origin, <url> is the URL of the remote warehouse.

For example: git remote add origin https://github.com/yinqishuo/TwinBee_Godot.git

Use the following command to push local changes to the remote repository:

git push <remote> <branch>

<remote> is the alias of the remote warehouse, <branch> is the name of the branch to be pushed.

Use the following command to pull changes from the remote repository:

git pull <remote> <branch>

This will pull the latest changes from the <remote> remote repository's <branch> branch and merge them into the current branch.

Use the following command to view the remote connection of the current warehouse:

git remote -v

Use the following command to cancel the connection to the remote repository:

git remote remove <remote>

Undo operation

Git has three commands that can be used to undo operations, namely git reset, git revert, git checkout. Their usage and differences are as follows:

Use the following command to undo the temporarily stored files. You can specify different parameters to control the degree of undoing. This command will change the commit history, so use it with caution.

git reset <arg> <commit_id>

<commit_id> is the ID of a certain submission that was withdrawn. You can use git log to query the ID. HEAD refers to the last submission ID, and HEAD~n refers to the last n submission IDs. <arg>There are 3, representing different degrees of undoing. The main objects affected are the work area, staging area and local warehouse.

  • --hard HEAD: Used to undo the last submission and the contents of the staging area, and the contents of the workspace will also be restored to the state of the last submission.

  • --soft HEAD: Used to undo the last submission, but retain the contents of the staging area and work area, and can resubmit.

  • --mixed HEAD: Used to undo the last submission and the contents of the staging area, but retain the contents of the workspace and can re-add and submit.

When we do not want to modify the submission history, we can use the following command to create a new submission, whose content is to undo the modifications made by the specified submission. This commandwill not change the commit history, so it is safer.

git revert  <commit_id>

Sometimes we just want to restore the modified workspace to the state of the uncommitted temporary storage area. Use the following command to Undo file modifications committed in the workspace

git checkout  <file>

<file>Specified files to be revoked. If you want to revoke all files, you can usegit checkout .

git tutorial - Zhihu (zhihu.com)

Guess you like

Origin blog.csdn.net/weixin_41099712/article/details/129629378