Git常用命令
工作区(写代码) == git add ==》 暂存区(临时存储) == git commit == 》 本地库(历史版本)
设置用户签名/邮箱
ws199@DESKTOP-2N1I9JA MINGW64 ~/Desktop $ git config --global user.name weishuo ws199@DESKTOP-2N1I9JA MINGW64 ~/Desktop $ git config --global user.email weishuo@weishuo.com
初始化本地库——git init
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo $ git init Initialized empty Git repository in D:/GIT/Git-Space/git.demo/.git/ ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master) $
查看本地库状态——git status
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master) $ git status On branch master #当前本地库分支位置 No commits yet #目前没有文件要提交 nothing to commit (create/copy files and use "git add" to track) #未提交,且无文件提交 ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master) $ git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) hello.txt nothing added to commit but untracked files present (use "git add" to track)
添加暂存区——git add
git add 文件名 |
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master) $ git add hello.txt warning: LF will be replaced by CRLF in hello.txt. The file will have its original line endings in your working directory ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master) $ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: hello.txt
文件删除暂存区——rm --cached
rm --cached 文件名 |
#删除暂存区,仍然存在工作区 ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master) $ git rm --cached hello.txt rm 'hello.txt'
提交本地库——git commit -m
将暂存区文件提交到本地库
git commit -m “日志信息” 文件名 |
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master) $ git commit -m "first commit" hello.txt warning: LF will be replaced by CRLF in hello.txt. The file will have its original line endings in your working directory [master (root-commit) 26da280] first commit 1 file changed, 7 insertions(+) create mode 100644 hello.txt ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master) $ git status On branch master nothing to commit, working tree clean
查看版本信息——git reflog
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master) $ git reflog 26da280 (HEAD -> master) HEAD@{0}: commit (initial): first commit #版本号 版本
查看版本详细信息——git log
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master) $ git log commit 26da28016adaee50fc76b4be71fbf2b4b3fda929 (HEAD -> master) Author: weishuo <weishuo@weishuo.com> Date: Tue Apr 12 21:53:00 2022 +0800 first commit
版本穿梭——git reset --hard 版本号
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master) $ git reset --hard ffce8cd HEAD is now at ffce8cd second commit ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master) $ git reflog ffce8cd (HEAD -> master) HEAD@{0}: reset: moving to ffce8cd 534cbd3 HEAD@{1}: commit: third commit ffce8cd (HEAD -> master) HEAD@{2}: commit: second commit 26da280 HEAD@{3}: commit (initial): first commit