The most commonly used git commands are: add Add file contents to the index bisect Find by binary search the change that introduced a bug branch List, create, or delete branches checkout Checkout a branch or paths to the working tree clone Clone a repository into a new directory commit Record changes to the repository diff Show changes between commits, commit and working tree, etc fetch Download objects and refs from another repository grep Print lines matching a pattern init Create an empty Git repository or reinitialize an existing one log Show commit logs merge Join two or more development histories together mv Move or rename a file, a directory, or a symlink pull Fetch from and integrate with another repository or a local branch push Update remote refs along with associated objects rebase Forward-port local commits to the updated upstream head reset Reset current HEAD to the specified state rm Remove files from the working tree and from the index show Show various types of objects status Show the working tree status tag Create, list, delete or verify a tag object signed with GPG
[root@t-luhx01-v-szzb git]# git reset HEAD test.txt Unstaged changes after reset: M test.txt [root@t-luhx01-v-szzb git]# git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)
modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
可以看到修改的内容已经退回到工作区了,撤销工作区的内容只需要再执行checkout即可
[root@t-luhx01-v-szzb git]# git checkout -- test.txt [root@t-luhx01-v-szzb git]# git status On branch master nothing to commit, working directory clean
回退版本
log选项能够查看我们的提交记录
[root@t-luhx01-v-szzb git]# git log --pretty=oneline 3fcf6a5878159e994c006db2dffc897e764ae33f Modify test.txt 837262c7ee7f7470c3b6d2f99a56c2d9737dd70f only test
[root@t-luhx01-v-szzb git]# git reflog 3fcf6a5 HEAD@{0}: reset: moving to 3fcf6a5 837262c HEAD@{1}: reset: moving to HEAD^ 3fcf6a5 HEAD@{2}: commit: Modify test.txt 837262c HEAD@{3}: commit: only test 51de587 HEAD@{4}: commit: only test 26db86b HEAD@{5}: commit (initial): only test
[root@t-luhx01-v-szzb git]# git reset --hard 3fcf6a5 HEAD is now at 3fcf6a5 Modify test.txt
删除文件
当我们删除文件后,查看状态能发现删除的操作记录
[root@t-luhx01-v-szzb git]# git status On branch master Changes not staged for commit: (use "git add/rm <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)
deleted: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
[root@t-luhx01-v-szzb git]# cat test.txt this is test file one line
通过add添加到暂存区,然后再进行第二次修改
[root@t-luhx01-v-szzb git]# git add test.txt [root@t-luhx01-v-szzb git]# cat test.txt this is test file one line two line
commit提交后,查看当前的状态
[root@t-luhx01-v-szzb git]# git commit -m "change context" [master ec0e82a] change context 1 file changed, 1 insertion(+) [root@t-luhx01-v-szzb git]# git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)
modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
[root@t-luhx01-v-szzb git]# git diff HEAD -- test.txt diff --git a/test.txt b/test.txt index 51f856e..4c806d2 100644 --- a/test.txt +++ b/test.txt @@ -1,2 +1,3 @@ this is test file one line +two line
[root@t-luhx01-v-szzb git]# git checkout -b dev Switched to a new branch 'dev' [root@t-luhx01-v-szzb git]# git branch * dev master [root@t-luhx01-v-szzb git]# vi test.txt [root@t-luhx01-v-szzb git]# echo "five line" >> test.txt [root@t-luhx01-v-szzb git]# git add test.txt \ > [root@t-luhx01-v-szzb git]# git commit -m "add five line" [dev f5de8d1] add three line 1 file changed, 1 insertion(+)
切回master分支再插入一行相同记录
[root@t-luhx01-v-szzb git]# git checkout master Switched to branch 'master' [root@t-luhx01-v-szzb git]# echo "six line" >> test.txt [root@t-luhx01-v-szzb git]# git add test.txt [root@t-luhx01-v-szzb git]# git commit -m "add six line" [master 8e81508] add three line 1 file changed, 1 insertion(+)
合并分区时出现错误
[root@t-luhx01-v-szzb git]# git merge dev Auto-merging test.txt CONFLICT (content): Merge conflict in test.txt Automatic merge failed; fix conflicts and then commit the result.
此时再看文件中的内容,列出了文件冲突的部分,我们需要手动处理
<<<<<<< HEAD six line ======= five line >>>>>>> dev
我们可以通过git log查看分支合并图
[root@t-luhx01-v-szzb git]# git log --graph --pretty=oneline --abbrev-commit * eb47c7c merge |\ | * e2dd8ec add five line * | 527d636 add six line |/ * 9b08349 1
[root@t-luhx01-v-szzb git]# git status On branch dev Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)
modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a") [root@t-luhx01-v-szzb git]# git stash Saved working directory and index state WIP on dev: e2dd8ec add five line HEAD is now at e2dd8ec add five line [root@t-luhx01-v-szzb git]# git status On branch dev nothing to commit, working directory clean
[root@t-luhx01-v-szzb git]# git stash list stash@{0}: WIP on dev: e2dd8ec add five line [root@t-luhx01-v-szzb git]# git stash apply stash@{0} On branch dev Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)
modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
[root@t-luhx01-v-szzb git]# git tag app-v1.0 [root@t-luhx01-v-szzb git]# git tag app-v1.0
当然,我们也可以针对指定的commit id来打标签
[root@t-luhx01-v-szzb git]# git log --pretty=oneline --abbrev-commit e2dd8ec add five line 9b08349 1 8cd159e add line 2c4226f add line e292e7d Merge branch 'dev' -m "merge" d4c71d5 add four line d94a43a add four line 56c52e2 Merge branch 'dev' 8e81508 add three line f5de8d1 add three line 18541da add two line ec0e82a change context 3fcf6a5 Modify test.txt 837262c only test 51de587 only test 26db86b only test [root@t-luhx01-v-szzb git]# git tag app-v0.5 8e81508 [root@t-luhx01-v-szzb git]# git tag app-v0.5 app-v1.0
查看tag信息
[root@t-luhx01-v-szzb git]# git show app-v1.0 commit e2dd8ec4b1944443ffb24ad3dbd8c1f21048b975 Author: luhengxing <690627803@qq.com> Date: Fri Aug 7 10:14:58 2020 +0800
add five line
diff --git a/test.txt b/test.txt index 70b872e..e767fe3 100644 --- a/test.txt +++ b/test.txt @@ -3,3 +3,4 @@ one line two line three line four line +five line