免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2697 | 回复: 3

git应用入门 [复制链接]

论坛徽章:
0
发表于 2012-02-24 08:39 |显示全部楼层
git应用入门





安装配置

Bash代码
  1. $ git config --global user.name "Your Name"  
  2. $ git config --global user.email your.email@example.com  

  3. $ git config --global user.name "Your Name"
  4. $ git config --global user.email your.email@example.com

  5. 使用co作为checkout的alias
复制代码
Java代码
  1. $ git config --global alias.co checkout  

  2. $ git config --global alias.co checkout

  3. 设置git的默认编辑器
复制代码
Java代码
  1. $ git config --global core.editor "subl -w"  

  2. $ git config --global core.editor "subl -w"

  3. Replace "subl -w" with "mate -w" for TextMate, "gvim -f" for gVim, or "mvim -f" for MacVim.

  4. 初始化git空文件夹

  5. Java代码  
  6. [code]$ git init  

  7. $ git init
  8. Initialized empty Git repository in /Users/mhartl/rails_projects/first_app/.git/
复制代码
配置rails命令生成的.gitignore文件




Java代码  
# Ignore bundler config   
/.bundle   
  
# Ignore the default SQLite database.   
/db/*.sqlite3   
  
# Ignore all logfiles and tempfiles.   
/log/*.log   
/tmp  

# Ignore bundler config
/.bundle

# Ignore the default SQLite database.
/db/*.sqlite3

# Ignore all logfiles and tempfiles.
/log/*.log
/tmp


Mac OS 如此配置[/code]
Java代码
  1. # Ignore bundler config   
  2. /.bundle   
  3.   
  4. # Ignore the default SQLite database.   
  5. /db/*.sqlite3   
  6.   
  7. # Ignore all logfiles and tempfiles.   
  8. /log/*.log   
  9. /tmp   
  10.   
  11. # Ignore other unneeded files.   
  12. doc/   
  13. *.swp   
  14. *~   
  15. .project   
  16. .DS_Store  

  17. # Ignore bundler config
  18. /.bundle

  19. # Ignore the default SQLite database.
  20. /db/*.sqlite3

  21. # Ignore all logfiles and tempfiles.
  22. /log/*.log
  23. /tmp

  24. # Ignore other unneeded files.
  25. doc/
  26. *.swp
  27. *~
  28. .project
  29. .DS_Store

  30. add和commit

  31. Java代码  
  32. $ git add .  

  33. $ git add .

  34. 查看状态

  35. Java代码  
  36. $ git status   
  37. # On branch master   
  38. #   
  39. # Initial commit   
  40. #   
  41. # Changes to be committed:   
  42. #   (use "git rm --cached <file>..." to unstage)   
  43. #   
  44. #       new file:   README.rdoc   
  45. #       new file:   Rakefile   
  46. ...  

  47. $ git status
  48. # On branch master
  49. #
  50. # Initial commit
  51. #
  52. # Changes to be committed:
  53. #   (use "git rm --cached <file>..." to unstage)
  54. #
  55. #       new file:   README.rdoc
  56. #       new file:   Rakefile
  57. ...
复制代码
提交命令

Java代码
  1. $ git commit -m "Initial commit"  
  2. [master (root-commit) df0a62f] Initial commit   
  3. 42 files changed, 8461 insertions(+), 0 deletions(-)   
  4. create mode 100644 README.rdoc   
  5. create mode 100644 Rakefile  

  6. $ git commit -m "Initial commit"
  7. [master (root-commit) df0a62f] Initial commit
  8. 42 files changed, 8461 insertions(+), 0 deletions(-)
  9. create mode 100644 README.rdoc
  10. create mode 100644 Rakefile
复制代码
(-m让你添加一条提交信息)

查看git日志


Java代码
  1. $ git log   
  2. commit df0a62f3f091e53ffa799309b3e32c27b0b38eb4   
  3. Author: Michael Hartl <michael@michaelhartl.com>   
  4. Date:   Thu Oct 15 11:36:21 2009 -0700  
  5.   
  6.   Initial commit   
  7. ...  

  8. $ git log
  9. commit df0a62f3f091e53ffa799309b3e32c27b0b38eb4
  10. Author: Michael Hartl <michael@michaelhartl.com>
  11. Date:   Thu Oct 15 11:36:21 2009 -0700

  12.   Initial commit
  13. ...
复制代码
关于回滚
Java代码
  1. $ git checkout -f   
  2. $ git status   
  3. # On branch master   
  4. nothing to commit (working directory clean)   
  5. $ ls app/controllers/   
  6. application_controller.rb  

  7. $ git checkout -f
  8. $ git status
  9. # On branch master
  10. nothing to commit (working directory clean)
  11. $ ls app/controllers/
  12. application_controller.rb
复制代码
(-f强制重写当前改变)

关于同步到远程github
Java代码
  1. $ git remote add origin git@github.com:<username>/first_app.git   
  2. $ git push origin master  

  3. $ git remote add origin git@github.com:<username>/first_app.git
  4. $ git push origin master
复制代码
关于git的分支
Java代码
  1. $ git checkout -b modify-README   
  2. Switched to a new branch 'modify-README'  
  3. $ git branch   
  4. master   
  5. * modify-README  

  6. $ git checkout -b modify-README
  7. Switched to a new branch 'modify-README'
  8. $ git branch
  9. master
  10. * modify-README
复制代码
(使用checkout的-b创建分支,git branch查看当前所有的分支,*说明当前的分支
git checkout -b modify-README创建分支并转换到当前分支

关于git的编辑功能
Java代码  
$ git mv README.rdoc README.md   
$ subl README.md  

$ git mv README.rdoc README.md
$ subl README.md
mv类似unix的mv

新的README.md
Java代码
  1. # Ruby on Rails Tutorial: first application   
  2.   
  3. This is the first application for  
  4. [*Ruby on Rails Tutorial: Learn Rails by Example*](http://railstutorial.org/)   
  5. by [Michael Hartl](http://michaelhartl.com/).   
  6.   
  7. $ git status   
  8. # On branch modify-README   
  9. # Changes to be committed:   
  10. #   (use "git reset HEAD <file>..." to unstage)   
  11. #   
  12. #       renamed:    README.rdoc -> README.md   
  13. #   
  14. # Changed but not updated:   
  15. #   (use "git add <file>..." to update what will be committed)   
  16. #   (use "git checkout -- <file>..." to discard changes in working directory)   
  17. #   
  18. #       modified:   README.md   
  19. #  

  20. # Ruby on Rails Tutorial: first application

  21. This is the first application for
  22. [*Ruby on Rails Tutorial: Learn Rails by Example*](http://railstutorial.org/)
  23. by [Michael Hartl](http://michaelhartl.com/).

  24. $ git status
  25. # On branch modify-README
  26. # Changes to be committed:
  27. #   (use "git reset HEAD <file>..." to unstage)
  28. #
  29. #       renamed:    README.rdoc -> README.md
  30. #
  31. # Changed but not updated:
  32. #   (use "git add <file>..." to update what will be committed)
  33. #   (use "git checkout -- <file>..." to discard changes in working directory)
  34. #
  35. #       modified:   README.md
  36. #
复制代码
提交
Java代码
  1. $ git commit -a -m "Improve the README file"  
  2. 2 files changed, 5 insertions(+), 243 deletions(-)   
  3. delete mode 100644 README.rdoc   
  4. create mode 100644 README.md  

  5. $ git commit -a -m "Improve the README file"
  6. 2 files changed, 5 insertions(+), 243 deletions(-)
  7. delete mode 100644 README.rdoc
  8. create mode 100644 README.md
  9. (-a提交所有改变)
复制代码
关于合并Merge
Java代码
  1. $ git checkout master   
  2. Switched to branch 'master'  
  3. $ git merge modify-README   
  4. Updating 34f06b7..2c92bef   
  5. Fast forward   
  6. README.rdoc     |  243 --------------------------------------------------   
  7. README.md       |    5 +   
  8. 2 files changed, 5 insertions(+), 243 deletions(-)   
  9. delete mode 100644 README.rdoc   
  10. create mode 100644 README.md  

  11. $ git checkout master
  12. Switched to branch 'master'
  13. $ git merge modify-README
  14. Updating 34f06b7..2c92bef
  15. Fast forward
  16. README.rdoc     |  243 --------------------------------------------------
  17. README.md       |    5 +
  18. 2 files changed, 5 insertions(+), 243 deletions(-)
  19. delete mode 100644 README.rdoc
  20. create mode 100644 README.md
复制代码
合并了所有changes后可以执行以下命令
Java代码  
$ git branch -d modify-README   
Deleted branch modify-README (was 2c92bef).  

$ git branch -d modify-README
Deleted branch modify-README (was 2c92bef).
用以删除合并了的分支,这步可选

Java代码
  1. # For illustration only; don't do this unless you mess up a branch   
  2. $ git checkout -b topic-branch   
  3. $ <really screw up the branch>   
  4. $ git add .   
  5. $ git commit -a -m "Major screw up"  
  6. $ git checkout master   
  7. $ git branch -D topic-branch  

  8. # For illustration only; don't do this unless you mess up a branch
  9. $ git checkout -b topic-branch
  10. $ <really screw up the branch>
  11. $ git add .
  12. $ git commit -a -m "Major screw up"
  13. $ git checkout master
  14. $ git branch -D topic-branch
复制代码
使用git branch -D 遗弃分支
(与-d不同,-D可以删除没有合并过的分支)

关于push
Java代码  
$ git push  

$ git push
在大多数系统上可以省略origin master直接使用git push
在某些系统上会出现问题
Java代码
  1. $ git push   
  2. fatal: The current branch master is not tracking anything.  

  3. $ git push
  4. fatal: The current branch master is not tracking anything.
复制代码

论坛徽章:
0
发表于 2012-02-25 11:34 |显示全部楼层
谢谢分享

论坛徽章:
0
发表于 2012-02-28 16:15 |显示全部楼层
github不错~

论坛徽章:
0
发表于 2012-02-28 20:33 |显示全部楼层
谢谢分享,收藏
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP