整理:按功能归类常用命令行
sudo apt-get install git
配置本地信息
git config --global user.name "xxxxxxxxxxx"
git config --global user.email "xxxxxx@xxxxx.com"
git config --list
# 如果没有 global 可以在 .git/config 中查看
配置ssh密钥
ssh-keygen -t rsa -C xxxxxxxxxxxxx@xxxxx.com
cat ~/.ssh/id_rsa.pub
粘贴到 https://github.com/settings/keys
测试
ssh -vT git@github.com
下面几种方法
# Method 1. git http + proxy http
git config --global http.proxy "http://127.0.0.1:1080"
git config --global https.proxy "http://127.0.0.1:1080"
# Method 2. git http + proxy shocks
git config --global http.proxy "socks5://127.0.0.1:1080"
git config --global https.proxy "socks5://127.0.0.1:1080"
# to unset
git config --global --unset http.proxy
git config --global --unset https.proxy
# Method 3. git ssh + proxy http
vim ~/.ssh/config
Host github.com
HostName github.com
User git
ProxyCommand socat - PROXY:127.0.0.1:%h:%p,proxyport=1087
# Method 4. git ssh + proxy socks
vim ~/.ssh/config
Host github.com
HostName github.com
User git
ProxyCommand nc -v -x 127.0.0.1:1080 %h %p
# 初始化
git init
# 添加修改的文件
git add README.md
# 提交版本
git commit -m "add 3 files."
创建远程仓库
上传本地内容
# 关联origin(默认的名字)到远程库
git remote add origin git@github.com:cuihanObject/cmdTest.git
git remote -v
# 推送版本
git push --set-upstream origin master
# 以后推送就只需要
git push
# 默认会补全缺省
git push origin master
==注意:这里需要配置好ssh密钥==
git add .
git commit -m "xxxxxxxxx"
git commit --amend (修改上次提交) --no-edit(不修改评论)
.git
,它就是版本库.git
里存了很多东西,其中最重要的就是称为stage
(或者叫index
)的暂存区add 和 commit 的操作如下
git add
实际上就是把文件修改添加到暂存区
git commit
提实际上就是把暂存区的所有内容提交到当前分支
(工作区 - 缓存区 - 提交区 )
git clone --depth 1 https://github.com/labuladong/fucking-algorithm.git
可以看到我们只克隆下包含最近一次commit的一个分支,这样这个项目文件就不会很大
# 查看HEAD全部的变化
git reflog
# –graph 图形
# –pretty=oneline 减少数据
# –abbrev-commit 头部数据减少
git log --graph --pretty=oneline --abbrev-commit
创建
# 创建
git branch dev
# 创建并切换新分支
git checkout -b dev
切换:工作空间切换
git checkout [版本] [文件]
回退:工作空间切换、==分支指向同时切换==
git reset --hard commit_id(可用 git log –oneline 查看)
《Git Reset 三种模式》 https://www.jianshu.com/p/c2ec5f06cf1a
删除
git branch -d dev
# 切换master分支
git checkout master
# 回退本地
git reset --hard 1f12413691b70d2d75d3c982823f2018ef193e33
# 强制提交远程
git push origin master -f
暂存
# 执行存储时,添加备注,方便查找
# 只有git stash 也要可以的,但查找时不方便识别。
git stash save “save message”
查看
# 查看stash了哪些存储
git stash list
# 显示做了哪些改动
# 默认show第一个存储,如果要显示其他存贮,后面加stash@{$num},比如第二个 git stash show stash@{1}
git stash show
恢复
# 应用某个存储,但不会把存储从存储列表中删除
# 默认使用第一个存储,即stash@{0},如果要使用其他个,git stash apply stash@{$num} , 比如第二个:git stash apply stash@{1}
git stash apply
# 应用某个存储,且会把存储从存储列表中删除
# 默认为第一个stash,即stash@{0},如果要应用并删除其他stash,命令:git stash pop stash@{$num} ,比如应用并删除第二个:git stash pop stash@{1}
git stash pop
删除
# 丢弃stash@{num}存储,从列表中删除这个存储
git stash drop stash@{num}
# 删除所有缓存的stash
git stash clear
==前提:pop时前应该把工作空间恢复到stash前的分支==
若分支经过提交,则可能发生冲突。pop时发生冲突后
git stash drop stash@{num}
《git merge和git merge –no-ff的区别》 https://www.cnblogs.com/damoblog/p/13144379.html
# Fast forward 模式(默认)
git merge dev
git merge dev --ff
# no-ff 模式(推荐)
git merge --no-ff -m "merge with no-ff" dev
区分几种模式
–squash 把一些不必要commit进行压缩,比如说,你的feature在开发的时候写的commit很乱,那么我们合并的时候不希望把这些历史commit带过来,于是使用–squash进行合并,此时文件已经同合并后一样了,但不移动HEAD,不提交。需要进行一次额外的commit来“总结”一下,然后完成最终的合并。
–no-ff(推荐) 关闭fast-forward模式,在提交的时候,会创建一个merge的commit信息,然后合并的和master分支merge的不同行为,向后看,其实最终都会将代码合并到master分支,而区别仅仅只是==master分支上简洁清晰==的问题;然后向前看,也就是我们使用reset的时候,就会发现,不同的行为就带来了不同的影响
如果发生冲突,使用status查看冲突位置,冲突修复后add进入暂存区消除冲突
git status
git add xxx
Step 1: From your project repository, check out a new branch and test the changes.
git checkout -b esirplayground-master
git pull https://github.com/esirplayground/AutoBuild-OpenWrt.git master
Step 2: Merge the changes and update on GitHub.
git checkout master
git merge --no-ff esirplayground-master
git push origin master
git diff --cached(工作区-缓存区)
git diff HEAD (工作区 - 缓存区 - 提交区 )
注释 #
#忽略所有.svn目录
.svn/
#忽略所有target目录
target/
#忽略所有.idea目录
.idea/
#忽略所有.iml文件
*.iml
语法 | 含义 :– | :– /
| 目录 *
| 多个字符 ?
| 单个字符 []
| 多个可选字符匹配单个字符 !
|不忽略(跟踪)匹配到的文件或目录
对于是否把文件进行版本管理,还有下面命令参数
添加版本管理 add -f
有些时候,你想添加一个文件到Git,但发现添加不了,原因是这个文件被.gitignore忽略了:
如果你确实想添加该文件,可以用-f强制添加到Git:
检察版本管理 check-ignore
或者你发现,可能是.gitignore写得有问题,需要找出来到底哪个规则写错了,可以用git check-ignore命令检查:
Git会告诉我们,.gitignore的第3行规则忽略了该文件,于是我们就可以知道应该修订哪个规则。
删除版本管理 rm
https://blog.csdn.net/sarafina527/article/details/104555842
.gitignore只能忽略那些原来没有被track(之前没有add过)的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。
git rm -r --cached target
git rm -r --cached .idea
此后不再追踪track这两个文件夹
git submodule add ~/submd/repos/lib1.git
git submodule
git submodule update
git submodule foreach git pull
git submodule foreach --recursive git submodule init
git submodule foreach --recursive git submodule updat
注意:
git submodule add
命令新增.gitmodules
文件
(记录了子模块的:名称、本地路径 path、网络路径 url)$ cat .gitmodules [submodule "libs/lib1"] path = libs/lib1 url = /home/henryyan/submd/repos/lib1.git
同时,
.git/config
文件的内容,最下面有submodule
的注册信息!$ git config --list --local ... submodule.libs/lib1.active=true submodule.libs/lib1.url=/home/henryyan/submd/repos/lib1.git
如果是子模块不是通过
add
添加,而是新拉的代码里面就有.gitmodules
。这时候.git/config
里是没有submodule
的注册信息的,这时候需要init
git submodule init
同时需要
update
把子模块下载下来git submodule update
拉仓库同时把子模块拉下来
git clone --recursive /path/to/repos/foo.git
# 逆初始化模块,其中{MOD_NAME}为模块目录,执行后可发现模块目录被清空
git submodule deinit {MOD_NAME}
# 删除.gitmodules中记录的模块信息(--cached选项清除.git/modules中的缓存)
git rm --cached {MOD_NAME}
# 提交更改到代码库,可观察到'.gitmodules'内容发生变更
git commit -am "Remove a submodule."
此外,你可能还需要删除
.git/modules/{MOD_NAME}
的缓存,否则无法创建同名的module.
.gitmodules
文件中对应模块的 url
属性git submodule sync
命令,将新的URL更新到文件 .git/config
.git/config
中对应模块的 url
属性被更新) (较低版本git可能不能自动更新 .git/config
文件,需要手动修改)clone tag fetch pull push rebase cherry-pick squash
- 参考:
廖雪峰的官方网站