Git操作小结
git操作小结,资料来源于网络
教程资源
在线教程(重点推荐)
资源
文章
git config
git配置文件~/.gitconfig
#查看帮助 git config --help git config --global user.name "Your Name Here" 其他常用config git config --global user.name "robbin" git config --global user.email "fankai#gmail.com" git config --global color.ui true #alias git config --global alias.co checkout git config --global alias.ci commit git config --global alias.st status git config --global alias.br branch git config --global core.editor "mate -w" # 设置Editor使用textmate git config -1 #列举所有配置
仓库基本操作
1.检出一个项目
git clone path-to-git-repository
2.更新本地仓库到最新改动
git pull
2.将现有项目推送到远程
cd test #跳到要提交的目录下 git init #初始化git git add . #将所有文件加入到索引 git commit #提交到HEAD git remote add origin <server> #增加到remote git push origin master #推送过去! DONE
3.丢弃本地所有改动与提交,获取服务器上最新版本并将主干分支指向它
git fetch origin git reset --hard origin/master
基本操作
1.添加(CurrentDir -> Stage)
git add file_name git add *
3.删除(CurrentDir -> Stage)
git rm file_name #从版本控制中删除,并删除磁盘上的文件 git rm --cached file_name #不删除磁盘上的
3.取消add/rm(Stage -> CurrentDir)
git reset HEAD file_name
2.提交(Stage -> HEAD)
git commit file_name -m '提交信息'
4.推送改动(HEAD -> Remote)
git push origin master
4.diff
git diff #diff CurrentDir Stage, 查看有哪些需要add git diff --cached/--staged #哪些需要commit
5.log
git log #查看日志
7.替换掉本地改动(Stage -> CurrentDir)
git checkout -- <filename>
8.mv
git mv old_name new_name #重命名
BEGIN: add -> commit -> push -> DONE
文件变化状态图
分支操作
1.创建分支
git branch new_br_name #创建 git branch #查看当前有的分支 git checkout new_br_name #切换到新分支 git checkout -b new_br_name #创建并切过去
2.远程分支
git push origin new_br_name #推送branch到远端维护起来 #删除 git push origin : <new_br_name> or git push origin --delete <new_br_name>
3.合并分支(merge)
git branch #查看当前分支 git checkout master #切换回主干 git merge new_br_name #合并new_br_name分支到主干, 自动合并 1.没冲突(no conflicts) -> fine -> commit -> push to remote 2.有冲突(conflicts) git diff #查看当前哪些文件有冲突, 标识 unmerged vim xxx #手动编辑解决冲突 冲突文件中标识 <<<<<<< HEAD:file.txt Hello world #当前branch的 ======= Goodbye #要合并branch的 >>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt git add xxx #加入 -> 解决所有冲突之后 -> commit -> push to remote
3.删除无用分支
git branch -d new_br_name #只能删除已经被当前分支合并的分支 git branch -D new_br_name #强删
4.撤销一个合并
git reset --hard HEAD
tag操作
BP:在发布之前,创建标签
1.创建删除
创建 git tag tag_name <commit ID> #commit ID可以通过git log来查看 删除 git tag -d tag_name
3.远程tag
推送所有tags git push --tags 删除 git push origin --delete tag <tag_name>
忽略某些文件
项目中那些不需要的文件(untracked),可以忽略
顶层工作目录中添加一个叫".gitignore"的文件 语法文档
# 以'#' 开始的行,被视为注释. # 忽略掉所有文件名是 foo.txt 的文件. foo.txt # 忽略所有生成的 html 文件, *.html # foo.html是手工维护的,所以例外. !foo.html # 忽略所有.o 和 .a文件. *.[oa]
常见问题
1.如何让git能处理汉字文件名
git默认quote任何非ascii文件名字符,想要支持非
git config --global core.quotepath false 或者在$HOME/.gitconfig 配置 [core] quotepath = false
2.git add -A 和 git add .的区别
"git add -A" = "git add .; git add -u". - git add -A stages All - git add . stages new and modified, without deleted - git add -u stages modified and deleted, without new
3.怎么配置git结果显示颜色
[color] #开启着色功能 status = auto diff = auto branch = auto interactive = auto
4.获得帮助
git help <command> # 显示command的help
其他
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" #然后使用 git lg
2.自己搭建代码库
类似github的应用,gitlab(google之)
3.git-flow
4.基于git的wiki
gollum(google之)
wklken
2013-12-01
版权声明:自由转载-非商用-非衍生-保持署名 | Creative Commons BY-NC-ND 3.0