Git常用操作
打开Git官网下载安装程序,按默认选项安装。
然后,打开资源管理器,任一目录位置鼠标右键菜单选择Git bash,弹出一个类似cmd的命令行窗口,证明安装成功。
安装完成后,需设置用户名和邮箱,在命令行输入以下代码:
git config --global user.name "Your Name"
git config --global user.email "email@qq.com"
通过如下命令把这个目录变成Git可以管理的仓库:
git init
用 git add 命令,把文件添加到仓库:
git add .
小点表示当前目录下所有文件。
用 git commit 命令,把文件提交到本地仓库:
git commit -m "first commit" //-m后面输入的是本次提交的说明,可以输入任意内容。
提交到远程仓库:
git remote add origin http://192.168.0.18:3000/name/xxxxx.git
现在,本地和远程各有一份完全一样的文件集。
当修改告一段落时,我们往往会创建一个新分支,如20220526,以便有问题时回滚到这一分支。使用命令 git checkout -b 完成这一功能,它
相当于把两条命令git branch
分支名、git checkout
分支名 合成一条,来实现一条命令新建分支+切换分支。
git checkout -b 20220526
Switched to a new branch '20220526'
将20220526分支推送到远端, -u
参数与--set-upstream
这一串是一个意思,所以用-u
就行,好记还好打。
git push -u origin 20220526
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcm/tlsverify for more information.
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcm/tlsverify for more information.
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a new pull request for '20220526':
remote: http://192.168.0.18:3000/zheng/server/compare/master...20220526
remote:
remote: . Processing 1 references
remote: Processed 1 references in total
To http://192.168.0.18:3000/zheng/server.git
* [new branch] 20220526 -> 20220526
branch '20220526' set up to track 'origin/20220526'.
此时远端有两个分支,分别是master和20220526,我们再切回master:
git checkout master
此时,如果本地作了提交,就可以通过命令把本地 master 分支的最新修改推送至远端master分支:
git push -u origin master
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcm/tlsverify for more information.
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcm/tlsverify for more information.
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 290 bytes | 290.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
remote: . Processing 1 references
remote: Processed 1 references in total
To http://192.168.0.18:3000/zheng/server.git
0f8fae1..f9e1b04 master -> master
branch 'master' set up to track 'origin/master'.
查看两分支的差异:
git log --left-right master...20220526 commit < f9e1b04d3fbf9a5592fb99936c7198287fcfdf30 (HEAD -> master, origin/master) Author: zheng <test@qq.com> Date: Fri May 27 13:03:40 2022 +0800 20220527
在本地修改一个文件,比如README.md。运行 git status 命令查看:
git status On branch 20220526 Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: README.md
上面的信息告诉我们,README.md 文件被修改了,但还没有提交修改。
如果我们想知道文件做了哪些修改,可使用git diff 命令查看。
添加要提交代码:
git add README.md
git commit -m "update no.1"
再用 git status 查看一下当前仓库状态:
git status On branch 20220526 nothing to commit, working tree clean
可以用git log命令查看改动历史:
git log commit 9059f3ad74a88f6d7bc07cd3b0fc9b0d822c1b41 (HEAD -> 20220526) Author: zheng <testqq.com> Date: Fri May 27 11:36:57 2022 +0800 update no.1 commit 1dc2e4e6d2706eafbc8e26c0df33c0d5f1dcb309 (origin/master, origin/20220526, master) Author: zheng <test@qq.com> Date: Fri May 27 07:52:23 2022 +0800 20220527 commit c75304d2e55e1702988165cfcedcdba712c3caf8 Author: zheng <test@qq.com> Date: Fri May 27 07:27:33 2022 +0800 first commit
我们还可以加上 --pretty=oneline 参数:
git log --pretty=oneline
这样可以单行显示提交记录
现在如果我们想把 README.md 文件退回到上一个版本,就可以使用 git reset 命令:
git reset --hard HEAD^ //HEAD表示当前版本,则HEAD^表示上一个版本,那么上上版本就是HEAD^^ HEAD is now at 1dc2e4e 20220527
如果要回到最新的版本,还是使用 git reset 命令。
查看远程仓库用git branch -a 命令,*表示当前仓库:
git branch -a * 20220526 master remotes/origin/20220526 remotes/origin/master