基础命令
Git命令
git --help #查看git的帮助文档
git [command] --help #查看某个具体命令的帮助文档,例如:git init --help
git init #在当前目录下初始化一个本地的空仓库,生成一个名为 .git 的子目录,但项目里的文件还没有被跟踪,git clone下来的项目不需要执行此操作。
git add . #把工作区内未跟踪的所有文件都添加到暂存区,*代表所有,也可以指定单独文件
命令实例:
root@DESKTOP-33IBDMI:/mnt/e/git# touch 1.txt
root@DESKTOP-33IBDMI:/mnt/e/git# ls
1.txt
root@DESKTOP-33IBDMI:/mnt/e/git# git add 1.txt
git status #查看git当前状态
命令实例:
root@DESKTOP-33IBDMI:/mnt/e/git# git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: 1.txt
git commit -m "此次提交的描述" #把所修改的文件提交到本地仓库
注:使用-m选项说写一段概要说明,所写得内容不要随便写,最好是说明commit后增加或修改了哪些功能。
命令实例:
root@DESKTOP-33IBDMI:/mnt/e/git# git commit -m "add 1.txt"
[master c42772f] add 1.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 1.txt
git rm --cache [文件名] #仅删除暂存区里的文件
命令实例:
root@DESKTOP-33IBDMI:/mnt/e/git# touch 2.txt
root@DESKTOP-33IBDMI:/mnt/e/git# git add 2.txt
root@DESKTOP-33IBDMI:/mnt/e/git# git rm --cache 2.txt
rm '2.txt'
root@DESKTOP-33IBDMI:/mnt/e/git# git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
2.txt
nothing added to commit but untracked files present (use "git add" to track)
git rm [文件名] #删除工作区的文件,commit时提交版本库
rm [文件名] #仅删除工作区的文件,commit时不会将该操作提交,需要加参数a才可以
命令实例:
root@DESKTOP-33IBDMI:/mnt/e/git# git rm 1.txt
rm '1.txt'
root@DESKTOP-33IBDMI:/mnt/e/git# git commit -m "rm 1.txt"
[master 1bef8f7] rm 1.txt
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 1.txt
root@DESKTOP-33IBDMI:/mnt/e/git# rm 2.txt
root@DESKTOP-33IBDMI:/mnt/e/git# git commit -m "rm 2.txt"
On branch master
Changes not staged for commit:
deleted: 2.txt
no changes added to commit
root@DESKTOP-33IBDMI:/mnt/e/git# rm 2.txt
root@DESKTOP-33IBDMI:/mnt/e/git# git commit -am "dele 2.txt"
[master 9a40f23] dele 2.txt
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 2.txt
git checkout [文件名] #从暂存区将文件恢复到工作区,如果工作区已经有该文件,则会选择覆盖
git checkout [分支名] [文件名] #从分支名拉取文件 并覆盖工作区里的文件
命令实例:
root@DESKTOP-33IBDMI:/mnt/e/git# touch 11.txt
root@DESKTOP-33IBDMI:/mnt/e/git# ls
11.txt
root@DESKTOP-33IBDMI:/mnt/e/git# cat 11.txt
root@DESKTOP-33IBDMI:/mnt/e/git# git add 11.txt
root@DESKTOP-33IBDMI:/mnt/e/git# git commit -m "add 11.txt"
[master ef5b5ee] add 11.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 11.txt
root@DESKTOP-33IBDMI:/mnt/e/git# echo 1111 >11.txt
root@DESKTOP-33IBDMI:/mnt/e/git# cat 11.txt
1111
root@DESKTOP-33IBDMI:/mnt/e/git# git checkout 11.txt
root@DESKTOP-33IBDMI:/mnt/e/git# cat 11.txt
root@DESKTOP-33IBDMI:/mnt/e/git#
热爱世间万物,无最爱,无例外、