对Linux中GIT相关操作做个总结
创建仓库
分为三种方式:
- 创建工程目录后$git init
- $git init 工程名称
- $git clone [url]:例如:git clone git@github.com:huguanghui/linux.git
- Linux上本地搭建Git服务器:
1 2
| git daemon --reuseaddr --export-all --enable=receive-pack --verbose --base-path=/root/GIT_local & ufw allow 9418
|
注意当git push出现reject报错时, 仓库目录下执行chmod -R 777 *
配置
- 显示当前配置:$git config - -list
- 编辑Git配置文件:$git config -e [- -global]
- 设置提交代码时的用户信息:
1 2
| git config [- -global] user.name "[name]" git config [- -global] user.email "[email address]"
|
Github的认证
文件相关操作
1
| git mv [file-original] [file-renamed]
|
代码提交
1
| git commit [file] .. -m [message]
|
- 如果代码没有任何变化,则用来改写上一次commit的提交信息
1
| git commit --amend -m [message]
|
分支
1
| git branch [new branch name]
|
1
| git checkout -b [new branch name]
|
1
| git branch [branch] [commit]
|
1
| git branch --track [branch] [remote-branch]
|
1
| git branch --set-upstream [branch] [remote-branch]
|
1
| git cherry-pick [commit]
|
1 2
| git push origin --delete git branch -dr
|
1
| git push origin 本地分支名:远程分支名
|
标签
1
| git push [remote] --tags
|
1
| git checkout -b [branch] [tag]
|
查看信息
- 显示commit历史,以及每次commit发生变化的文件
1 2
| git log --follow [file] git whatchanged [file]
|
仓库拉取
1
| git pull <远程主机名> <远程分支名>:<本地分支名>
|
仓库同步
1 2
| git push -u <远程主机名> <本地分支名>:<远程分支名> git push origin 本地:master
|
远程同步
1 2
| git remote add origin git@github.com:huguanghui/hexo_blog.git git push -u origin master
|
1
| git remote show [remote]
|
submodule使用
查看子模块的状态
1
| $git submodule 或 git submodule status
|
添加子模块
1
| $git submodule add git@github.com:name/*.git subpath
|
删除子模块
1 2 3
| // 1.删除.gitmodules中需要删除的模块 // 2.删除目录 $git rm --cached subpath
|
克隆带子模块的工程
1 2 3 4 5 6
| // 方法一 $git clone git@github.com:name/*.git $git submodule init $git submodule update // 方法二 $git clone --recurse-submodules git@github.com:name/*.git
|
更新子模块
1 2 3 4 5 6 7 8
| // 1.更新所有子模块到最新版本 $git submodule update --remote // 2.更新指定子模块到最新版本 $git submodule update --remote <submodule-name> // 3.提交子模块的依赖 $git add * $git commit -m "refresh" $git push
|
环境配置
推荐子模块单独维护s
1 2 3 4 5
| // .gitmodules [submodule "abc"] path = abc url = git@github.com:huguanghui/cplusplus.git ignore = dirty/untracked/all
|
撤销
1
| git checkout [commit] [file]
|
- 重置暂存区的指定文件,与上一次的commit保持一致,工作区不变
- 重置当前分支的指针为commit,同时重置暂存区,工作区不变
1
| git reset --hard [commit]
|
1
| git reset --keep [commit]
|
过滤
版本回退
未使用git add
1 2
| git checkout -- filepathname 放弃单个文件修改 git checkout . 放弃所有文件修改
|
使用了git add
1 2
| git reset HEAD filepathname git reset HEAD .
|
使用了git commit
1 2
| git reset --hard HEAD^ 退回上一次Commit的状态 git reset --hard commitid 退回对应id的的位置
|
账号和密码自动保存配置
注意事项:<源地址>:<目标地址>