Git common commands --- git commit command

git commit mainly commits the changes in the staging area to the local repository. Every time we use the git commit command, we generate a 40-digit hash value in the local repository, which is also called commit-id.

The commit-id is very useful when the version is rolled back, it is equivalent to a snapshot, which can be returned to here at any time in the future by combining the command with git reset.

 

1. git commit -m "message"

The -m parameter means that the following message can be entered directly. If the -m parameter is not added, the message cannot be entered directly, but an editor, usually vim, will be called to allow you to enter the message.

When the message we want to submit is long or we want to describe it more clearly and concisely, we can use this format:


2. git commit -a -m "message"

The -a parameter means that all tracked files that perform modification or deletion operations can be committed to the local repository, even if they are not added to the staging area by git add.

Note that newly added files (that is, files not managed by the git system) cannot be submitted to the local repository. It is recommended not to use the -a parameter in general. For normal submission, use git add to first add the files to be changed to the staging area, and then use git commit to submit them to the local repository.


3. git commit --amend

git commit --amend is also called append commit, it can append the newly modified code to the previous commit-id without adding a new commit-id.

1) If the latest version in the repository is the version we want to add, it is the easiest at this time, modify the workspace code directly, then git add, and then you can directly git push to the server, in the middle No other operations such as git pull are required.

2) If the latest version in the repository is not the version we want to append, then we need to roll back the version in the repository to the version we want to append.

      2.1> First, if we know that the version we need is separated by n commits from the latest version, then we can directly use the git reset --hard HEAD~n command, and run the git log -1 command after execution. You will find that the latest version in the repository is the version we need. At this time, you can directly modify the code in the workspace. After the modification, perform git add, execute the git commit --amend command, and then git push.

      2.2> Second, if we don't know that there are n commits between the version we need and the latest version, then we can use git log to view the commit-id in the repository and find the commit-id we need. , execute git reset --hard commit-id in the terminal, run the git log -1 command after execution, we will find that the latest version in the repository is the version we need, and then modify the code directly in the workspace , after the change, perform git add, then execute the git commit --amend command, and then git push.

 

4. git commit --help

View help

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326284791&siteId=291194637
Recommended