Git The third lecture on the basic operation of Git

Basic operations of Git

When using Git for version control, there are some basic operations that must be mastered. This chapter describes these basic operations, including how to add and commit files, view and compare changes, undo changes, delete files, and more.

Add and submit files

Git uses a staging area (or index) to manage changes to files. Adding and committing files is one of the most commonly used operations in Git.

add files

To add a file to Git's staging area, you can use the following command:

git add <文件名>

For example, to example.txtadd a file named to the staging area, you can run the following command:

git add example.txt

Submit documents

Committing files is the process of permanently saving the changes in the staging area to the Git repository.

git commit -m "提交信息"

In the above command, -moptions are used to specify the submitted information. The commit message should clearly describe what the commit is about.

For example, to commit the file you just added example.txt, you can run the following command:

git commit -m "添加示例文件 example.txt"

View and compare changes

Git provides a variety of commands to view and compare file changes, helping us understand the differences between the working directory and the warehouse.

View change status

To see the status of changes to files in your working directory, you can use the following command:

git status

This will show information about all modified but not yet staged files, staged but not yet committed files, and untracked files.

compare changes

To compare the changed contents of the files, you can use the following command:

git diff <文件名>

This will show the changes to the specified file in the working directory and the staging diff.

For example, to compare example.txtfile changes, you can run the following command:

git diff example.txt

undo changes

Sometimes we may need to undo changes to files, Git provides some commands to help us roll back to the previous state.

Undo changes in the working directory

To undo changes to files in your working directory, you can use the following command:

git checkout -- <文件名>

This will undo the changes to the specified files and revert to the state of the last commit.

For example, to undo example.txtchanges to a file, run the following command:

git checkout -- example.txt

Undo the changes in the staging area

To undo changes to files in the staging area, you can use the following command:

git reset HEAD <文件名>

This will unstage the specified file, returning it to an unstaged state.

For example, to unstage example.txta file, run the following command:

git reset HEAD example.txt

unsubmit

To undo the last commit, you can use the following command:

git revert HEAD

This will create a new commit that undoes the changes from the last commit.

Delete Files

To delete a file from Git, you can use the following command:

git rm <文件名>

This will remove the specified file from Git's version control.

Please note that after executing the above command, the file will be deleted and cannot be restored, please proceed with caution.

For example, to delete example.txta file named , you can run the following command:

git rm example.txt

The above is a brief introduction to the basic operations of Git. After mastering these basic operations, you can start using Git for version control and team collaboration.

switch commit branch

Use git checkoutthe command and the commit ID of that commit to switch to the specified commit.

git checkout <commitID>

基于commitID创建新分支
git branch 新分支 commitId
git checkout 新分支

Guess you like

Origin blog.csdn.net/huanglu0314/article/details/131157172