git
普通命令
git init 初始化
自动创建一个分支 master,以及只想master 的一个指针 HEAD
git init
git add 添加文件
添加文件到 暂存区
git add xxxx.file
git commit 提交
保存在本地分支,就是本地 master 上。
git commit -m xxx
命令 | 解析 |
---|---|
-m | 注解 |
git log 查看历史
查看提交历史记录
git log --pretty=oneline
命令 | 解析 |
---|---|
–pretty=oneline | 显示格式 |
git reflog 查看所有命令历史
用于往后跳转时,查找log
git reflog
git reset 回退版本
查看回退版本
git reset --hard commitId
命令 | 解析 |
---|---|
–hard HEAD^ or commitId | 回退到某个版本,HEAD:当前版本,HEAD:上一个版本,HEAD^:上上个版本,HEAD~100:第100个版本 |
git diff 查看版本之间的不同
git diff HEAD -- Readme.md
命令 | 解析 |
---|---|
HEAD – fileName | 查看 fileName 跟上个版本的不同 |
git checkout 把工作区的修改丢弃
git checkout
命令 | 解析 |
---|---|
– fileName | fileName 的工作区的修改全部丢弃 |
标签命令
对某一时间点的代码打上标签,发布版本时要用到。
git tag
为某个分支当打上 tag,方便发布等。
// show all tags
git tag
// light tag
git tag -a 'v1.0.0'
// tag the code
git tag -a 'v1.0.0' -m 'this is tag'
// delete the tag
git tag -d 'v1.0.0'
命令 | 含义 |
---|---|
-l ‘tag_regx’ | 过滤,可以使用通配符 |
-a ‘tag_name’ | 打上标签 |
-m ‘commone’ | 加上注释 |
-s | 使用 GPG 来打标签 |
-v | 验证标签 |
-d | 删除tag |
git show
显示标签的详细说明
// show detail of the tag
git show 'tag_name'
git tag -a version log_id
如果打tag 之后,发现还有没提交的代码,可以提交之后,添加到tag中
// check log
git log
// add log to the tag
git tag -a v1.0.0 log_id
git push origin tagname
提交tag 需要显式提交,提交到远程服务器。
// push the tag
git push origin v1.0.0
// push all the tags
git push origin --tags
git push origin :refs/tags/v1.0.0
删除远程tag
// delete the remote tag
git push origin :refs/tags/v1.0.0
git checkout tag
切换到指定 tag ,很容易丢失代码,一般使用tag代码创建分支开发。
// checkout the tag, dangerous
git checkout v1.0.0
// create a new branch use the tag
git checkout -b branch1 tag
合并分支
git merge
git merge branch1
参数 | 解析 |
---|---|
branch1 | 需要合并的分支 |
第三方git server 操作
把本地代码上传到 github
- 进去本地代码文件夹,git init
- git add .
- git commit -m “comm”
- git remote add origin https://github.com/TimingTop/birmingham.git
- git pull --rebase origin master
- git pull origin master --allow-unrelated-histories (可选操作)
- git push -u origin master
Q&A
1. 如果 .gitignore 不生效
原因是文件之前被加入追踪了,要清除掉,才能生效
git rm -r --cached .
git add .
git commit -m 'update .gitignore'