学习知识点:
https://juejin.im/post/599e14875188251240632702
学习git的游戏好帮手-GitHug的安装:
http://os.51cto.com/art/201207/350825.htm
GitHug闯关攻略(两篇结合起来看比较好):
http://www.jianshu.com/p/482b32716bbe
http://blog.csdn.net/huitailang1991/article/details/54289701
小知识点总结:
1. 配置别名用以显示不同颜色的log,特别赞,使用时,直接输入git lg命令即可:
DavidtekiMacBook-Air:~ du$ git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset-%C(yellow)%d%Creset%s%Cgreen(%cr)%C(bold blue)<%an>%Creset'--abbrev-commit"
2. git blame DavidtekiMacBook-Air:GitTest du$ git blame README
^88a80bc (duhongliu 2017-08-30 15:01:59 +0800 1) i am a teacher
^88a80bc (duhongliu 2017-08-30 15:01:59 +0800 2) you are my sunshine
3065d0dd (duhongliu 2017-08-30 15:09:19 +0800 3) hahakuankuandoctor
^88a80bc (duhongliu 2017-08-30 15:01:59 +0800 4) oo
3. git shortlog 根据提交者的名字进行分组显示,-e显示邮件信息和提交者及提交信息,-s只显示提交数量和提交者,-n按照提交数量大小对提交者和提交信息进行排序
DavidtekiMacBook-Air:GitTest du$ git shortlog
duhongliu (2):
first commit
second commit
4. gitk是和git一起安装的,which gitk查看gitk安装位置,输入gitk命令,会出现图形化界面
DavidtekiMacBook-Air:GitTest du$ gitk
5. git reset
执行git add之后,文件进入暂存区,使用git reset HEAD
执行git add和git commit之后,文件进入本地仓库,使用git reset
6. git stash 与 git pop,AndroidStudio中的Version Control中有Shelve Changes和Unshelve功能,与上述功能类似。
git stash //暂存当前的改动
git stash save "修改说明信息" //暂存当前的改动
git stash list //暂存的改动列表
git stash apply //恢复暂存的所有改动
git stash apply stash@{num} //恢复暂存的某个改动,num为通过git stash list查出的编号
git stash pop //将git stash栈中最后一个版本取出来恢复
7. 在GitHub中创建一个Repository后,将本地的项目进行上传时,运行以下两个命令
DavidtekiMacBook-Air:GitTest du$ git remote add origin [email protected]:Kikitious/TestGit.git
DavidtekiMacBook-Air:GitTest du$ git push -u origin master
我遇到的问题是:第二行命令输入后,显示failed to push some refs to '[email protected]:Kikitious/TestGit.git'
DavidtekiMacBook-Air:GitTest du$ git push -u origin master
To github.com:Kikitious/TestGit.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:Kikitious/TestGit.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
出现这个错误的原因是因为远程库中的文件没有下载下来(创建Repository时有README文件)。此时应该运行git pull origin master命令:
DavidtekiMacBook-Air:GitTest du$ git pull origin master
From github.com:Kikitious/TestGit
* branch master -> FETCH_HEAD
fatal: refusing to merge unrelated histories
又出现了refusing to merge unrelated histories错误,什么原因呢?因为他们是两个不同的项目,要把两个不同的项目合并,需要这样写git pull origin master --allow-unrelated-histories,此处参考: http://blog.csdn.net/lindexi_gd/article/details/52554159
DavidtekiMacBook-Air:GitTest du$ git pull origin master --allow-unrelated-histories
From github.com:Kikitious/TestGit
* branch master -> FETCH_HEAD
Merge made by the 'recursive' strategy.
README.md | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 README.md
这时就将远程Repository中的代码pull下来了,此时再执行push命令:
DavidtekiMacBook-Air:GitTest du$ git push -u origin master
Counting objects: 23, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (17/17), done.
Writing objects: 100% (23/23), 2.06 KiB | 703.00 KiB/s, done.
Total 23 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), done.
To github.com:Kikitious/TestGit.git
bf38f7c..75c0ced master -> master
Branch master set up to track remote branch master from origin.
大功告成。之后的对文件的操作流程就是add--commit--push了。
8. git tag
打个本地标签,一般在Master上打标签。打tag时,用git tag
DavidtekiMacBook-Air:GitTest du$ git tag v0.1
DavidtekiMacBook-Air:GitTest du$ git tag v0.1 1f3cbfe0973f6790ceed8ebb5997b5b4273096c6
将标签push到远程仓库上,用git push origin
DavidtekiMacBook-Air:GitTest du$ git push origin v0.1
Total 0 (delta 0), reused 0 (delta 0)
To github.com:Kikitious/TestGit.git
* [new tag] v0.1 -> v0.1
查看本地标签,用git tag
DavidtekiMacBook-Air:GitTest du$ git tag
v0.0
v0.1
v0.2
删除本地标签,用git tag -d DavidtekiMacBook-Air:GitTest du$ git tag -d v0.1
Deleted tag 'v0.1' (was bda6e27)
删除远程仓库的标签,先删除本地标签,再用git push origin :refs/tags/DavidtekiMacBook-Air:GitTest du$ git tag -d v0.1
Deleted tag 'v0.1' (was bda6e27)
DavidtekiMacBook-Air:GitTest du$ git push origin :refs/tags/v0.1
To github.com:Kikitious/TestGit.git
- [deleted] v0.1