Git教程 - 安装Git、建仓库、添加和推送文件至库
git 2016-08-23 11:33:39

GIT教程参照:

http://www.itbulu.com/10-minutes-git.html

http://www.bootcss.com/p/git-guide/

 

git 直接克隆指定分支:

PHP Code复制内容到剪贴板
  1. git clone -b 分支名 git地址  

 

查看提交历史日志:

git log --oneline 

 

安装拉到最下面

C/C++ Code复制内容到剪贴板
  1. //查看全局配置  
  2. git config --global -l  
  3. //设置全局用户名  
  4. git config --global user.name "****_0926"  
  5. //设置全局邮箱  
  6. git config --global user.email "****@qq.com"  
  7. //设置全局密码  
  8. git config --global user.password "****0926"  

 

例1:

C/C++ Code复制内容到剪贴板
  1. //初始化新仓库  
  2. git init   
  3.   
  4. //将远程仓库命名为demo  
  5. git remote add demo https://git.coding.net/wujin_0926/demo.git  
  6.   
  7. //查看当前添加的远程仓库  
  8. git remote -v  
  9.   
  10. //要抓取所有 demo 有的,但本地仓库没有的信息  
  11. git fetch demo  
  12.   
  13. //将远程仓库的重命名,将origin更名为demo  
  14. git remote rename origin demo     
  15.   
  16. //将远程仓库origin移除  
  17. git remote rm origin  
  18.   
  19. //查看更改的内容  
  20. git diff  
  21.   
  22. //提交更新到本地仓库,且加上本次提交注释                                              
  23. git commit -m '测试提交的说明'    
  24.   
  25. //将本地仓库中的数据推送到远程仓库的本地命名的demo里的master分支                       
  26. git push demo master  
  27.   
  28. //更新本地仓库的代码到最新                                 
  29. git pull      
  30.   
  31. //删除已跟踪的文件,保留本地文件                                          
  32. git rm --cached path/file    
  33.        
  34. //这个命令会忽略一个文件,即使这个文件已经存在git库中,每次修改都不会提示                      
  35. git update-index --assume-unchanged path/file      
  36.      
  37. //放弃本地所有修改,将远程仓库的文件下载到本地  
  38. git fetch origin master  
  39.   
  40. //假如你想要丢弃你所有的本地改动与提交,可以到服务器上获取最新的版本并将你本地主分支指向到它:  
  41. git fetch origin  
  42. git reset --hard origin/master  

 

 

git update-index --assume-unchanged 的真正用法是这样的:

你正在修改一个巨大的文件,你先对其 git update-index --assume-unchanged,这样 Git 暂时不会理睬你对文件做的修改;
当你的工作告一段落决定可以提交的时候,重置改标识:git update-index --no-assume-unchanged,于是 Git 只需要做一次更新,这是完全可以接受的了;
提交+推送。

 

初始化以后报错:

error: failed to push some refs to 'xxxxx.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

可以通过如下命令进行代码合并【注:pull=fetch+merge]

PHP Code复制内容到剪贴板
  1. git pull --rebase origin master  

 

 

 例2:

C/C++ Code复制内容到剪贴板
  1. //拉取远程仓库中的文件到本地当前的demo文件夹下  
  2. git clone https://git.coding.net/****_0926/demo.git demo  
  3.   
  4. //命令 ,查看当前工作目录下的文件状态  
  5. git status  
  6.   
  7. //跟踪所有文件  
  8. git add --all  
  9.   
  10. //提交更新到本地仓库,且加上本次提交注释  
  11. git commit -m '测试提交的说明'  
  12.   
  13. //将本地仓库中的数据推送到远程仓库  
  14. git push demo master  
  15.   
  16. //拉取更新  
  17. git pull  

 

 git 忽略目录: 

C/C++ Code复制内容到剪贴板
  1. #编辑.gitignore文件(vim .gitignore)  
  2.   
  3. #忽略所有文件,注意放在开头  
  4. ~install  
  5. logs  
  6. .gitignore  
  7. admin/config.php  
  8. config.php  
  9. bak/  
  10. image/cache  
  11. #除folder1文件夹外  
  12. !/folder1  
  13. #除folder2文件夹外  
  14. !/folder2   
  15. #忽略的文件或目录 一行一个  

只能忽略未上传到git上的目录,如果 忽略已经上传的,再次修改后但不提交的,请使用上面的 

C/C++ Code复制内容到剪贴板
  1. //这个命令会忽略一个文件,即使这个文件已经存在git库中,每次修改都不会提示,path/file改为自己的路径               
  2. git update-index --assume-unchanged path/file  

 

清除git初始化:

PHP Code复制内容到剪贴板
  1. rm -rf .git  

 

git报错:

 QQ图片20170815142727.png

解决办法:

PHP Code复制内容到剪贴板
  1. git rm -rf E:/WWW/dteols/dteols-mobile-gzh/.git/refs/remotes/origin/gzh.lock  

 

Git 仓库地址修改办法

1、先删,再加

PHP Code复制内容到剪贴板
  1. git remote rm origin  
  2. git remote add origin [url]  

 

2、直接修改地址

PHP Code复制内容到剪贴板
  1. git remote set-url origin [NEW_URL]  

 

git pull冲突:

hint: Pulling without specifying how to reconcile divergent branches is

hint: discouraged. You can squelch this message by running one of the following

hint: commands sometime before your next pull:

hint: 

hint:   git config pull.rebase false  # merge (the default strategy)

hint:   git config pull.rebase true   # rebase

hint:   git config pull.ff only       # fast-forward only

hint: 

hint: You can replace "git config" with "git config --global" to set a default

hint: preference for all repositories. You can also pass --rebase, --no-rebase,

hint: or --ff-only on the command line to override the configured default per

hint: invocation.

 

该问题就是,我本地有commit,但是未提交,origin也有新的commit,并且冲突

如果这里使用rebase为true,就在pull的时候一步步合并代码,并生成新的commit再提交

目前还是使用merge比较好,全局设置

C/C++ Code复制内容到剪贴板
  1. git config --global pull.rebase false  

 

查看是否生效:

C/C++ Code复制内容到剪贴板
  1. git config --global --list  
  2.   
  3. ...  
  4.   
  5. ## 有这一行表示生效  
  6. pull.rebase=false  

 

 


 

建项目目前我尝试的就是在coding

https://coding.net/

 

tortoisegit管理工具下载(含语言包)

https://tortoisegit.org/download/

先下载安装程序,再下载语言包,最后在setting里选择使用的语言包

QQ截图20161021160611.jpg

一般都是64位吧

 

 

 

本文来自于:http://www.yoyo88.cn/study/git/34.html

上一篇 返回列表
下一篇 关于分支
Powered by yoyo苏ICP备15045725号