1 Introduction
In the process of using Git, after each code modification is completed, the modified content needs to be saved to a local branch, and then pushed to the remote branch, and each commit will be in the form of a commit Save to the branch, users can use the command line to view, edit and modify the submitted content, or perform corresponding operations on the git operation interface, and can also directly download all the code content corresponding to the submission, as follows As shown in the figure, this article mainly introduces Git's commit-related operations from the perspective of command line operations
2. Use
(1) Submit
After completing the code modification, you can use git status
to view the basic situation of the code change, or you can use git diff
to view the detailed change content of the changed file. When it is confirmed that it is correct, you can use git add .
to add all the content to be submitted, or you can select several files git add filename
to Add a file. After the submission is completed, you can use git commit -m "comment"
to complete the submission of the content, so that the submitted content enters the local branch. When the submission prompt content is longer, you can open the vim editor to increase the content, and then git commit
open the vim editor. After the content is added, save and exit. The
following figure shows the working path of Git. Project is a local file (also known as a workspace). It can be git add
added to the staging area, by git commit
adding to the repository (in the local branch), and by git push
adding to In the remote branch, git pull
the content on the remote branch can be pulled to the local branch through
(2) View
When we git add
add files to the temporary storage area with the help of git diff
( git diff 文件名
) , we can view the changes of all (some) files in this submission. After we have git commit
added the changes to the repository, we can use git show 版本id
To see the changes under this version
(3) Modification
①Modify submission information
Scenario 1: Modify the submission information of the latest commit
git commit --amend
, enter the commit editing interface, and save after editing.
Scenario 2: Modify the submission information of the previous n commits.
Suppose you need to modify the submission information of the last nth commit.
$ git rebase -i HEAD~n
# 进入编辑模式,会出现类似以下的内容
pick 6608e22 修改代码结构调整导致不能正常显示的问题
pick 1d381cd 菜单切换可用
...
# 将需要修改的commit的那一行的pick修改为edit,然后保存退出
# 然后输入
$ git commit --amend
# 进入你需要修改的commit编辑界面,编辑后保存退出
# 修改结束后,输入以下命令返回到最新的commit
$ git rebase --continue
Note that you have to modify a few commit, will perform several times git commit --amend
and git rebase --continue
operation
② modify the submission
step1: the current branch will be temporarily stored non-work state
git stash
step2: Move the HEAD to the commit that needs to be modified
git rebase eb69bff96^ --interactive
step3: find the need to modify commit, will be the first line pick
change edit
saved after
step4: start modifying contents of the file
step5: Add the remaining changes to the file
git add
step6: Append changes to submission
git commit --amend
step7: Move HEAD back to the latest commit
git rebase --continue
step8: restore the previous working state
git stash pop
(4) Delete
For example, my submission history is as follows
commit 58211e7a5da5e74171e90d8b90b2f00881a48d3a
Author: test <[email protected]>
Date: Fri Sep 22 20:55:38 2017 +0800
add d.txt
commit 0fb295fe0e0276f0c81df61c4fd853b7a000bb5c
Author: test <[email protected]>
Date: Fri Sep 22 20:32:45 2017 +0800
add c.txt
commit 7753f40d892a8e0d14176a42f6e12ae0179a3210
Author: test <[email protected]>
Date: Fri Sep 22 20:31:39 2017 +0800
init
If you want to delete the add c.txt
commit 0fb295fe0e0276f0c81df61c4fd853b7a000bb5c
whose remark is commit
step1. First find the commit submitted before this submission7753f40d892a8e0d14176a42f6e12ae0179a3210
step2. Execute the following command
git rebase -i 7753f40
The following interface pops up (the original picture is lost, the picture below is similar)
step3. The 0fb295f
front of this line pick instead drop, and then follow the prompts to exit the save
step4. So far, the specified commit has been deleted, you can use it to git log
view
(5) Merger
Sometimes multiple commits with similar meanings in a branch will mess up the entire commit history. At this time, part of the commit can be merged into one commit to beautify the entire commit history. You can use the rebase method to merge multiple times. Commit, the main steps are as follows:
Step1: git log to view the current commit history
For example, it is necessary to merge the following 3 commits of "Leave Application Client Code Optimization" into one commit;
Step2: git rebase for git compression
execute git rebase -i HEAD~4 to rebase the last 4 commits;
The commands below are very clear for specific operations. You can use the squash and fixup commands for commit merging. The difference is that squash will add the comment of the commit to the previous commit comment, and fixup is to abandon the comment of the current commit;
Save and exit after editing. Git will automatically compress the commit history. If there is a conflict, remember to use git rebase --continue to return to the current git compression process after resolving the conflict;
Step3: Push to the remote warehouse git push -f
to review the commit history, you will find that these commits have been merged, and the entire commit history is much more concise:
(6) Push to the remote
①Do not set tracking
git push origin t6 ②Set
tracking
git push --set-upstream origin branch name
git push
③Forced push
git push -f is
often used to rebase, merge, delete, and modify the submitted content