基本命令
命令名称 | 作用 |
---|---|
git config --global user.name username | 设置用户签名 |
git config --global user.email 邮箱 | 设置用户签名 |
git init | 初始化本地库 |
git status | 查看本地库状态 |
git add filename | 添加到暂存区 |
git commit -m "这里填写注释" filename | 提交到本地库 |
git log | 查看历史记录 |
git reset --hard 版本号 | 切换版本 |
设置用户签名,首次安装git后,必须要配置一下用户签名,否则不能提交代码。
bashadmin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master) $ git config --global user.name username admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master) $ git config --global user.email [email protected] admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master) $ git config user.name username admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master) $ git config user.email [email protected]
初始化本地库,执行 git init
命令后,会在当前目录生成一个 .git
的文件夹。
bashadmin@DESKTOP-NIGB69Q MINGW64 /d/git_test
$ git init
Initialized empty Git repository in D:/git_test/.git/
使用 git status
查看本地库状态,新建一个 file01.txt
文件,再查看状态,就会显示有一个文件未被追踪了。
bashadmin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
file01.txt
nothing added to commit but untracked files present (use "git add" to track)
git add filename
命令添加到暂存区,可以使用 git rm --cached filename
bashadmin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master)
$ git add file01.txt
warning: in the working copy of 'file01.txt', LF will be replaced by CRLF the next time Git touches it
admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: file01.txt
暂存区的文件提交到本地仓库,提交后就会形成一个历史版本。语法格式git commit -m "first commit" filename
。
bashadmin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master)
$ git commit -m "first commit" file01.txt
warning: in the working copy of 'file01.txt', LF will be replaced by CRLF the next time Git touches it
[master (root-commit) a12d1af] first commit
1 file changed, 2 insertions(+)
create mode 100644 file01.txt
git reflog
和 git log
命令可以查询版本信息。添加一些 git log --oneline --graph
参数,可以更简洁的显示。
bashadmin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master)
$ git reflog
a12d1af (HEAD -> master) HEAD@{0}: commit (initial): first commit
admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master)
$ git log
commit a12d1af85e8844216b4128c37a7720dcef1ae0fb (HEAD -> master)
Author: username <[email protected]>
Date: Thu Aug 3 06:05:44 2023 +0700
first commit
admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master)
$ git log --oneline --graph
* 73d9797 (HEAD -> master) merge test
|\
| * 6136283 (hot-fix) change hot-fix
* | b3825f8 change master
* | 16ad4dc Merge branch 'hot-fix'
|\|
| * cb11ce3 hot-fix change
* | 7ce02f2 master
|/
* 3590acd hot-fix first commit
* 819f640 second commit
* a12d1af first commit
切换版本使用 git reset --hard 版本号
命令,底层就是更改 HEAD
指针的指向。
bashadmin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master) $ git reflog 59fa421 (HEAD -> master) HEAD@{0}: commit: third commit 819f640 HEAD@{1}: commit: second commit a12d1af HEAD@{2}: commit (initial): first commit admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master) $ git reset --hard 819f640 HEAD is now at 819f640 second commit
常用命令
命令名称 | 作用 |
---|---|
git branch 分支名 | 创建分支 |
git branch -v | 查看分支 |
git checkout 分支名 | 切换分支 |
git merge 分支名 | 把指定分支合并到当前分支 |
git branch -v
命令查看分支
bashadmin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master) $ git branch -v * master 819f640 second commit
git branch 分支名
可以创建新分支
bashadmin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master) $ git branch hot-fix admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master) $ git branch -v hot-fix 819f640 second commit * master 819f640 second commit
使用 git checkout 分支名
可以切换分支,加 -b
参数可以创建并切换到分支
bashadmin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'
admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (hot-fix)
$ git branch -v
* hot-fix 819f640 second commit
master 819f640 second commit
把 hot-fix
分支合并到 master
分支
bashadmin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'
admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (hot-fix)
$ cat file01.txt
hello git 11111
this is test
this is test
this is test
this is test
this is test
admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (hot-fix)
$ git checkout master
Switched to branch 'master'
admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master)
$ cat file01.txt
hello git 11111
this is test
admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master)
$ git merge hot-fix
Updating 819f640..3590acd
Fast-forward
file01.txt | 4 ++++
1 file changed, 4 insertions(+)
admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master)
$ cat file01.txt
hello git 11111
this is test
this is test
this is test
this is test
this is test
代码冲突时候合并分支会报错,需要手动合并,手动合并分支提交到代码库时候,不能指定分支名,否则会报错。
bashadmin@DESKTOP-NIGB69Q MINGW64 /d/git_test (hot-fix)
$ cat file01.txt
hello git 11111
this is test
this is test
this is test hot-fix change
this is test 12311
this is test abc
admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (hot-fix)
$ git checkout master
Switched to branch 'master'
admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master)
$ cat file01.txt
hello git 11111
this is test
this is test
this is test
this is test
this is test master change
admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master)
$ cat file01.txt
hello git 11111
this is test
this is test
this is test
this is test
this is test master change
admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master)
$ git merge hot-fix
Auto-merging file01.txt
CONFLICT (content): Merge conflict in file01.txt
Automatic merge failed; fix conflicts and then commit the result.
admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master|MERGING)
$ vim file01.txt
admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master|MERGING)
$ git add file01.txt
admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master|MERGING)
$ git commit -m "merge test" file01.txt
fatal: cannot do a partial commit during a merge.
admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master|MERGING)
$ git commit -m "merge test"
[master 73d9797] merge test
admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master)
$ cat file01.txt
hello git 11111
this is test
this is test
this is test hot-fix change
this is test 12311
this is test master change
常用命令
命令名称 | 作用 |
---|---|
git remote -v | 查看所有远程地址的别名 |
git remote add 别名 远程地址 | 创建远程地址别名 |
git push 别名 本地分支 | 推送本地分支上的内容到远程仓库 |
git clone 远程地址 | 将远程仓库克隆到本地 |
git pull 别名 远程分支名 | 将远程仓库最新内容合并到本地分支 |
git查看、添加和删除远程地址别名。
bashadmin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master) $ git remote add git_test [email protected]:username/test.git admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master) $ git remote -v git_test [email protected]:username/test.git (fetch) git_test [email protected]:username/test.git (push) admin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master) $ git remote remove git_test
推送本地仓库的到远程仓库, cat ~/.ssh/id_rsa.pub
查看本机公钥,把公钥配置到GitHub账号之后再进行推送。
bashadmin@DESKTOP-NIGB69Q MINGW64 /d/git_test (master) $ git push git_test master
拉取远程仓库到本地仓库
bashadmin@DESKTOP-NIGB69Q MINGW64 /d/work/code/git_test/test (main)
$ git clone [email protected]:username/test.git
本文作者:柯南
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!