首页 > 代码库 > Git教程
Git教程
本地测试git:
http://lvwzhen.github.io/Git-Tutorial/ Git教程
每个机器都必须自报家门:你的名字和Email地址
$ git config --global user.name "Your Name" $ git config --global user.email "email@example.com"
创建一个空目录
$ mkdir learngit $ cd learngit $ pwd /Users/michael/learngit
通过git init命令把这个目录变成Git可以管理的仓库
$ git init Initialized empty Git repository in /Users/michael/learngit/.git/
编写一个readme.txt文件,内容如下:
Git is a version control system.
Git is free software.
用命令git add告诉Git,把文件添加到仓库:
$ git add readme.txt
用命令git commit告诉Git,把文件提交到仓库:
$ git commit -m "wrote a readme file" [master (root-commit) cb926e7] wrote a readme file 1 file changed, 2 insertions(+) create mode 100644 readme.txt
运行git status命令看看结果:
$ git status # On branch master # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: readme.txt #no changes added to commit (use "git add" and/or "git commit -a")
可以通过命令:
$ git push origin master
创建dev分支,然后切换到dev分支:
$ git checkout -b dev Switched to a new branch ‘dev‘ git checkout命令加上-b参数表示创建并切换,相当于以下两条命令: $ git branch dev $ git checkout dev Switched to branch ‘dev‘
git branch命令查看当前分支:
$ git branch * dev master
可以在dev分支上正常提交,比如对readme.txt做个修改,加上一行:
Creating a new branch is quick.
然后提交:
$ git add readme.txt $ git commit -m "branch test" [dev fec145a] branch test 1 file changed, 1 insertion(+)
现在,dev分支的工作完成,我们就可以切换回master分支:
$ git checkout master Switched to branch ‘master‘
现在,我们把dev分支的工作成果合并到master分支上:
$ git merge dev Updating d17efd8..fec145a Fast-forward readme.txt | 1 + 1 file changed, 1 insertion(+)
合并完成后,就可以放心地删除dev分支了:
$ git branch -d dev Deleted branch dev (was fec145a).
删除后,查看branch,就只剩下master分支了:
$ git branch * master
$ git config --global color.ui true
这样,Git会适当地显示不同的颜色
忽略某些文件时,需要编写.gitignore
#Mac .DS_Store
告诉Git,以后st就表示status:
$ git config --global alias.st status
co表示checkout,ci表示commit,br表示branch:
$ git config --global alias.co checkout $ git config --global alias.ci commit $ git config --global alias.br branch
提交就可以简写成:
$ git ci -m "bala bala bala..."
git 错误 fatal: This operation must be run in a work tree
git config --bool core.bare true
1 查看远程分支
git branch -a
2 查看本地分支
git branch
3 创建分支
git branch test
4 切换分支到test
git checkout test
5 删除本地分支
git branch -d test
7 删除远程版本
git branch -r -d origin/branch-name git push origin :branch-name
本文出自 “技术蓝” 博客,请务必保留此出处http://bstdn.blog.51cto.com/8667020/1897596
Git教程