git常用的命令汇总
声明
本文中的内容属于个人总结整理而来,个人水平有限,对于部分细节难免有理解错误及遗漏之处,如果您在阅读过程中有所发现,希望您能指正,同时文章中的部分内容也参考了其它大神的文章,如果文章中的内容侵犯了您的权益,表示非常歉意,请您指出,我将尽快修改。
简介
本文中对常用的git的命令的使用方法进行了一个汇总。
目前有一部分操作还尚未完善,后续将会持续更新
变更说明
- [1.0.0] - 初次内容整理 - 2021-11-23
用户相关操作
一般操作都分为全局及局部操作,一般情况下命令中带有–global的表示针对全局的配置进行设置,如果只是想对局部生效去掉–global即可
-
查看及修改局部用户名和邮箱地址
$ git config user.name
$ git config user.email
$ git config user.name "XXXXX"
$ git config user.email "XXX@XXX"
-
查看全局用户名和邮箱地址
$ git config --global user.name
$ git config --global user.email
$ git config --global user.name "XXXXX"
$ git config --global user.email "XXX@XXX"
-
设置大小写敏感
$ git config --global core.ignorecase
$ git config --global core.ignorecase false
-
配置GitBash中显示中文 如果本地文件或者文件夹为中文时,中文的字符不能正常显示。或者使用git log查看日志时,带有中文的日志无法正常显示的话,可以通过如下操作处理
$ git config --global core.quotepath false
$ git --no-pager log
$ git config --global core.pager more
-
修改默认的合并及比较工作为Beyond Compared 详细参见官方文档:Using Beyond Compare with Version Control Systems
$ git config --global diff.tool bc
$ git config --global difftool.bc.path "C:/Program Files/Beyond Compare 4/BComp.exe"
$ git difftool <filePath>
$ git config --global merge.tool bc
$ git config --global mergetool.bc.path "C:/Program Files/Beyond Compare 4/BComp.exe"
$ git mergetool <filePath>
$ git config --global mergetool.keepbackup false
分支相关操作
-
创建本地分支 在本地创建名称为testing的分支,创建后并不会切换到对应的分支 *符号指向的为当前所在的分支 $ git branch testing
$ git branch
* master
testing
-
创建并切换本地分支 分支创建完成后会自动切换到对应的分支上
$ git branch
master
* testing
$ git checkout -b testing123
Switched to a new branch 'testing123'
$ git branch
master
testing
* testing123
-
本地切换到指定的分支
$ git branch
* master
testing
$ git checkout testing
Switched to branch 'testing'
$ git branch
master
* testing
-
切换到远程指定的分支
$ git branch -a
* master
remotes/origin/DotEntity
remotes/origin/HEAD -> origin/master
remotes/origin/master
$ git checkout -b DotEntity origin/DotEntity
Switched to a new branch 'DotEntity'
Branch 'DotEntity' set up to track remote branch 'DotEntity' from 'origin'.
$ git branch -a
* DotEntity
master
remotes/origin/DotEntity
remotes/origin/HEAD -> origin/master
remotes/origin/master
-
查看本地分支 *符号指向的为当前所在的分支 $ git branch
master
* testing
-
查看本地与远程仓库的所有分支 *符号指向的为当前所在的分支,以remotes/开头的分支为远程仓库的分支
$ git branch -a
master
* testing
remotes/origin/HEAD -> origin/master
remotes/origin/master
-
查看远程仓库分支
$ git branch -r
origin/HEAD -> origin/master
origin/master
-
将本地分支与远程仓库分支关联 某些时候使用git pull时会出现如下情况: There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> release
其原因是:git在本地创建一个分支后,需要与远程分支关联,如果没有关联,git执行操作时会提示添加关联,关联的目的是为了在执行git pull,git push操作时不再需要指定对应的远程分支 解决办法: #将本地的分支与远程仓库的分支关联,remote-branch对应的远程仓库的分支,local-branch对应的当前的本地分支
$ git branch --set-upstream-to=origin/<remote-branch> local-branch
$ git branch --set-upstream-to=origin/DotEntity DotEntity
Branch 'DotEntity' set up to track remote branch 'DotEntity' from 'origin'.
-
将本地创建的分支推送到远程仓库上
$ git branch testing
$ git branch -a
* master
testing
remotes/origin/HEAD -> origin/master
remotes/origin/master
$ git push origin testing
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'testing' on GitHub by visiting:
remote: https://github.com/XXXX/GitLeanProj/pull/new/testing
remote:
To https://github.com/XXXX/GitLeanProj.git
* [new branch] testing -> testing
$ git branch -a
* master
testing
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/origin/testing
-
删除本地分支 如果当前在将要删除的分支上,最好在删除前切换到其它的分支上
$ git branch -d testing123
Deleted branch testing123 (was aa9ae1c).
-
删除远程仓库分支
$ git branch -a
master
* testing
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/origin/testing
$ git push --delete origin testing
To https://github.com/XXX/GitLeanProj.git
- [deleted] testing
$ git branch -a
master
* testing
remotes/origin/HEAD -> origin/master
remotes/origin/master
-
本地与远程仓库进行同步 $ git fetch
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 8 (delta 4), reused 0 (delta 0)
Unpacking objects: 100% (8/8), done.
From ssh://XXX/ZZZ/XXX
e9419c21..c7eaeede develop -> origin/develop
-
本地向远程仓库进行拉取 $ git pull
Updating 1c3ec93e..9ff1beaa
Fast-forward
.../FlatBuffer/AddBindLinkTargetEffectData.cs | 26 ++++++++++--------
11 files changed, 84 insertions(+), 77 deletions(-)
delete mode 100644 XXX/XX/XX/TargetSecondType.cs
Current branch feature/PROJECTEM-5916--技能升级 is up to date.
-
合并指定的分支
$ git merge 5916-opt-linkeffect
Updating 25f4ab3e..f46df244
Fast-forward
.../Timeline/Actions/Effects/EffectActionUtil.cs | 126 ++++++++++++---------
1 file changed, 73 insertions(+), 53 deletions(-)
-
查看所有分支最后一次提交
$ git branch -v
* master 09a37b1 Initial commit
testing1 24065ff add new file
testing2 904c97d delete file
-
简单比较两个分支提交差异情况
$ git log branch1 ^branch2
$ git log branch1..branch2
$ git log branch1...branch2
$ git log --left-right branch1...branch2
$ git log --left-right DotEntity...master
commit < 01fa8f5fba8b7705c2c60f5ec385b87a4355f66e (HEAD -> DotEntity, origin/DotEntity)
Author: yubin.li <liyubin.724@163.com>
Date: Tue May 26 19:27:28 2020 +0800
new:准备处理Avatar相关的逻辑
-
比较任意两分支差异(git diff)
$ git diff DotEntity master --stat
Dot/DotEditor/DotEditor.csproj | 6 -
Dot/DotEditor/Entity/Node/NodeBehaviourEditor.cs | 177 +--------------
Dot/DotEngine/DotEngine.csproj | 10 -
Dot/DotEngine/Entity/Avatar/AvatarUtil.cs | 2 +-
DotProject/Assets/DotEngine/DotEngine.dll | Bin 171008 -> 163840 bytes
...
21 files changed, 7 insertions(+), 854 deletions(-)
$ git diff branch1 branch2 <filePath>
$ git diff branch1 branch2
提交相关的操作
合并相关的操作
$ git merge NativeDrawer-AddFieldButton
Updating d1fdec2..de795b3
Fast-forward
Dot/DotEditor/DotEditor.csproj | 1 +
.../NativeDrawer/Decorator/BoxedHeaderDrawer.cs | 2 +-
.../NativeDrawer/Decorator/ButtonDrawer.cs | 48 +++++++++++++++++++++
.../NativeDrawer/Decorator/DecoratorDrawer.cs | 4 +-
19 files changed, 99 insertions(+), 17 deletions(-)
create mode 100644 Dot/DotEditor/NativeDrawer/Decorator/ButtonDrawer.cs
create mode 100644 Dot/DotEngine/NativeDrawer/Decorator/ButtonAttribute.cs
-
取消合并
$ git merge --abort
-
冲突后启动合并工具
$ git mergetool
This message is displayed because 'merge.tool' is not configured.
See 'git mergetool --tool-help' or 'git help config' for more details.
'git mergetool' will now attempt to use one of the following tools:
opendiff kdiff3 tkdiff xxdiff meld tortoisemerge gvimdiff diffuse diffmerge ecmerge p4merge araxis bc codecompare emerge vimdiff
Merging:
DotProject/Assets/DotEngine/DotEngine.dll
DotProject/Assets/DotEngine/DotEngine.dll.mdb
DotProject/Assets/DotEngine/DotEngine.pdb
DotProject/Assets/DotEngine/Editor/DotEditor.dll
DotProject/Assets/DotEngine/Editor/DotEditor.dll.mdb
DotProject/Assets/DotEngine/Editor/DotEditor.pdb
Normal merge conflict for 'DotProject/Assets/DotEngine/DotEngine.dll':
{local}: modified file
{remote}: modified file
Hit return to start merge resolution tool (vimdiff):
-
解决二进制类型的文件冲突 二进制类型的文件一旦冲突后是无法合并的
$ git checkout FILE --ours{--theirs}
变基(rebase)相关操作
清理相关的操作
$ git clean -n
$ git clean -f <path>
$ git clean -df
$ git clean -xf
$ git reset --hard
$ git clean -df
撤销本地修改
对于新增加文件来说,由于新文件并未添加到git的记录中,属于未被tracked的状态,撤销修改时不会对其有任何影响,如果确认不需要的话,直接删除即可
$ git checkout .
$ git checkout [filename]
重置相关操作(git reset)
$ git reset --hard [hashcode]
贮存相关的操作
贮藏(stash)会处理工作目录的脏的状态——即跟踪文件的修改与暂存的改动——然后将未完成的修改保存到一个栈上, 而你可以在任何时候重新应用这些改动(甚至在不同的分支上)。
可以参见更详细的:贮藏与清理
$ git stash [push] <message>
Saved working directory and index state On DotEntity: 贮存目前修改优先解决NativeDrawer的BUG
$ git stash list
stash@{0}: On DotEntity: 贮存目前修改优先解决NativeDrawer的BUG
$ git stash show
Dot/DotEditor/NativeDrawer/NativeDrawerObject.cs | 8 ++++++++
DotProject/Assets/DotEngine/DotEngine.dll | Bin 171008 -> 171008 bytes
...
7 files changed, 8 insertions(+)
$ git stash show -p
diff --git a/Dot/DotEditor/NativeDrawer/NativeDrawerObject.cs b/Dot/DotEditor/NativeDrawer/NativeDrawerObject.cs
index 0d2937c..b309db3 100644
--- a/Dot/DotEditor/NativeDrawer/NativeDrawerObject.cs
+++ b/Dot/DotEditor/NativeDrawer/NativeDrawerObject.cs
@@ -59,6 +59,14 @@ public void OnGUILayout()
EditorGUILayout.EndScrollView();
...
}
diff --git a/DotProject/Assets/DotEngine/DotEngine.dll b/DotProject/Assets/DotEngine/DotEngine.dll
index 5ace8d9..dc92d15 100644
...
$ git stash apply
$ git stash pop
$ git stash drop stash@{$num}
$ git stash clear
重命名文件或文件夹
-
基本的重命名(移动)操作 $ git mv <old name> <new name>
-
存储库中已有指定文件夹需要覆盖 $ git mv -f <old name> <new name>
-
区分大小写的重命名 由于默认的情况下Git不区分大小写,如果需要进行重命名时,需要先将文件或者文件夹命名为其它的名称,再次重命名到指定的名称 $ git mv casesensitive tmp
$ git mv tmp CaseSensitive
关于Git大小写的问题有:
- stackoverflow大小写问题描述
fork相关的操作
参考资料
|