GIT学习

福尔摩斯对我笑 2018-06-13 28746 次 2590条

  初次使用GIT前的配置

git config --global user.name "用户名"
git config --global user.email "邮箱"
git init

git add 文件名            //开始检测文件
git commit -m 修改说明    //提交版本

git status命令可以让我们时刻掌握仓库当前的状态

git checkout -- 文件名称    //暂存区域数据恢复到工作区域
git log  //文件修改历史  如果嫌输出信息太多,看得眼花缭乱的,可以试试加上--pretty=oneline参数

git reset --mixed HEAD~   //移动HEAD的指向,将其指向上一个快照; 将HEAD移动后的快照回滚到暂存区域
git reset --soft HEAD~   //移动HEAD的指向,将其指向上一个快照 (相当于撤销操作)
git reset --hard HEAD~      //移动HEAD的指向,将其指向上一个快照;将HEAD移动后指向的快照回滚到暂存区域;将暂存区域的文件还原到工作目录

git reset --hard  ID号     //回滚指定快照(可回滚到之前,需指定ID号)
git reset 版本快照  文件名/路径     //回滚个别文件
git reflog用来记录你的每一次命令

查看文件不同的地方

git diff HEAD -- 文件名
命令git checkout -- readme.txt意思就是,把readme.txt文件在工作区的修改全部撤销,这里有两种情况:

文件差异对比

git diff    //比较暂存区域与工作目录
git diff ID号 ID号    //比较两个历史快照
git diff 快照ID    //比较当前工作目录和Git仓库中的快照
git diff --cached 【快照ID】 //比较暂存区域和Git仓库快照

修正最近一次提交的描述

git commit --amend

使用场景

场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file。
场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD <file>,就回到了场景1,第二步按场景1操作。
场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考版本回退一节,不过前提是没有推送到远程库。

远程仓库链接GITHUB

第1步:创建SSH Key。在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsaid_rsa.pub这两个文件,如果已经有了,可直接跳到下一步。如果没有,打开Shell(Windows下打开Git Bash),创建SSH Key:

ssh-keygen -t rsa -C "youremail@example.com"

在用户主目录里找到.ssh目录,里面有id_rsaid_rsa.pub两个文件,这两个就是SSH Key的秘钥对,id_rsa是私钥,

第2步:登陆GitHub,打开“Account settings”,“SSH Keys”页面进行填写

第3步:将本地代码推送到github

git remote add origin git@github.com:SeekHappy/git.git

git push -u origin master //推送到远程仓库

注意: 由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。

小结

要关联一个远程库,使用命令git remote add origin git@server-name:path/repo-name.git

关联后,使用命令git push -u origin master第一次推送master分支的所有内容;

此后,每次本地提交后,只要有必要,就可以使用命令git push origin master推送最新修改;

分布式版本系统的最大好处之一是在本地工作完全不需要考虑远程库的存在,也就是有没有联网都可以正常工作,而SVN在没有联网的时候是拒绝干活的!当有网络的时候,再把本地提交推送一下就完成了同步,真是太方便了!

码云公匙配置:https://segmentfault.com/a/1190000002737818

常用命令:https://www.cnblogs.com/allanli/p/git_commands.html  很全面

问题:

  1. 当出现no changes added to commit时如何正确使用git提交命令

    1.   到解决方案根目录下:git add .                  ("."是必须要的)

    2.   git commit -m "some word"

    3.   git push -u origin master 

分支管理

查看分支:git branch
创建分支:git branch <name>
切换分支:git checkout <name>
创建+切换分支:git checkout -b <name>
合并某分支到当前分支:git merge <name>
删除分支:git branch -d <name>

首先,我们创建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

切换分支

git checkout master  //切换到主分支,更改master 即可切换其他分支

分支合并

git merge dev    //吧dev分支合并到master    注意:合并到那个分支,取决于你当前在那个分支

删除分支

git branch -d dev   //删除dev分支

GIT提交时屏蔽目录

创建.gitignore文件,使用另存为的方式,不然无法保存

*.a       # 忽略所有 .a 结尾的文件
!lib.a    # 但 lib.a 除外
/TODO     # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
build/    # 忽略 build/ 目录下的所有文件
doc/*.txt # 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt
---------------------

1)配置语法:

以斜杠“/”开头表示目录;

以星号“*”通配多个字符;

以问号“?”通配单个字符

以方括号“[]”包含单个字符的匹配列表;

以叹号“!”表示不忽略(跟踪)匹配到的文件或目录;

此外,git 对于 .ignore 配置文件是按行从上到下进行规则匹配的,意味着如果前面的规则匹配的范围更大,则后面的规则将不会生效;

删除远程分支和本地分支

1、 使用命令git branch -a 查看所有分支 , 其中,remote/origin/master表示的是远程分支

2、  删除远程分支   

    

使用命令 git push origin --delete dev 可以删除远程分支dev

3、  删除本地分支

git branch -d Chapater8 可以删除本地分支(在主分支中)

git 忽略已经被提交的文件

有时候在 .gitignore 文件中添加规则并未生效,原因是 .gitignore 只能忽略原来没有被 track 的文件,如果某些文件已经加入了版本管理中,修改 .gitignore 是无效的。解决方法:先把不想记录文件的本地缓存删除,再在 .gitignore 中添加忽略该文件,然后再提交。

git rm -r --cached xxx   //xxx表示不再想版本控制的文件,然后在  .gitignore 文件中加入该忽略的文件
git add .
git commit -m 'update .gitignore'

博文: https://www.cnblogs.com/xuecanmeng/p/7383171.html

https://www.cnblogs.com/yongdaimi/p/7600052.html

GIT设置大小写敏感

git config core.ignorecase false  

本地分支名称修改

git branch -m oldName newName

删除远程分支

git push --delete origin name 

本地分支提交到远程

git push origin name:newName 

多个分支时,如何将同一次提交到多个分支

1、在git commit -m xxx 之后,找到 commit ID, 记下来。

2、 切换到另外一个分支,执行命令

git cherry-pick commitID

3、在两个分支中分别push代码即可

设置本地分支追踪远程分支

git push --set-upstream origin

之后就可以直接使用

git push提交代码

GIT配置多个仓库地址: https://blog.csdn.net/hao495430759/article/details/80673568

标签: 无

非特殊说明,本博所有文章均为博主原创。

头像也是LOGO
未来博客 博主大人
到了这个年纪,不应该再彷徨!

发表评论



 


已有 2590 条评论 1


  1. kobe 11

    I am commenting to let you know what a nice encounter my cousin’s child experienced reading through the blog. She even learned many issues, with the inclusion of what it’s like to possess an ideal helping spirit to get other folks smoothly have an understanding of chosen tricky things. You really surpassed her expected results. Thank you for giving such informative, trustworthy, revealing and also fun thoughts on your topic to Sandra.

    回复 2019年10月24日
  2. jordan shoes

    I simply wanted to appreciate you yet again. I’m not certain the things I would’ve taken care of in the absence of the actual tips and hints documented by you on this topic. Previously it was the alarming problem in my opinion, but seeing a new expert mode you treated that forced me to weep with happiness. I am thankful for the information as well as expect you really know what a great job that you’re doing teaching the others through your web site. Probably you have never encountered all of us.

    回复 2019年10月24日
  3. vapormax

    Thank you a lot for giving everyone an extremely splendid opportunity to read in detail from here. It is often very superb and as well , full of a lot of fun for me personally and my office acquaintances to visit the blog at a minimum thrice every week to read the fresh stuff you have got. And of course, I am also certainly fascinated with all the outstanding advice you give. Certain 1 facts in this article are surely the simplest we have had.

    回复 2019年10月23日
  4. jordan 12

    I simply desired to thank you very much once more. I’m not certain the things that I might have made to happen in the absence of the smart ideas documented by you on my concern. It truly was a real difficult difficulty in my position, but understanding your well-written mode you handled that took me to jump with contentment. I’m grateful for your information and have high hopes you comprehend what a powerful job that you are providing educating many others through your webpage. Most probably you haven’t encountered all of us.

    回复 2019年10月23日
  5. adidas stan smith women

    I definitely wanted to post a simple comment so as to say thanks to you for some of the marvelous advice you are posting here. My incredibly long internet search has at the end of the day been rewarded with professional points to share with my close friends. I would declare that most of us visitors are unequivocally fortunate to live in a useful network with very many lovely people with beneficial guidelines. I feel very much blessed to have come across your web page and look forward to really more thrilling minutes reading here. Thank you once more for all the details.

    回复 2019年10月21日
  6. christian louboutin shoes

    I’m just writing to let you know of the beneficial experience my friend’s child gained browsing the blog. She learned such a lot of details, not to mention what it is like to possess an awesome coaching heart to let others quite simply grasp specified complicated subject matter. You actually exceeded visitors’ desires. Many thanks for distributing the valuable, safe, explanatory and easy guidance on that topic to Tanya.

    回复 2019年10月20日
  7. balenciaga trainers

    My spouse and i felt comfortable Jordan managed to complete his basic research through the precious recommendations he got while using the weblog. It is now and again perplexing to simply possibly be releasing guides that many others may have been selling. We really do know we have got the blog owner to thank for this. The most important illustrations you made, the straightforward web site navigation, the relationships your site aid to foster – it’s mostly excellent, and it’s really making our son and the family feel that the situation is fun, which is certainly extraordinarily mandatory. Thank you for the whole thing!

    回复 2019年10月19日
  8. nike air max

    Needed to write you that little bit of remark so as to say thanks a lot over again regarding the lovely concepts you have provided on this site. It has been certainly seriously open-handed of people like you to give unhampered what exactly many of us would’ve marketed for an ebook to make some dough for themselves, especially considering that you might have tried it in the event you decided. Those points in addition worked to become easy way to know that other people have the same eagerness really like my own to know a lot more with respect to this issue. I know there are a lot more pleasurable sessions up front for those who looked at your site.

    回复 2019年10月18日
  9. air max 95

    I am glad for writing to make you be aware of what a remarkable encounter my cousin’s princess found browsing your blog. She mastered numerous pieces, most notably what it’s like to have a great giving heart to make other people quite simply grasp specific complicated issues. You actually exceeded our desires. I appreciate you for delivering the interesting, safe, revealing and in addition fun thoughts on the topic to Evelyn.

    回复 2019年10月17日
  10. pg 1

    I simply needed to appreciate you all over again. I do not know the things that I could possibly have worked on in the absence of these ideas contributed by you directly on my area of interest. This was a very alarming issue in my opinion, however , observing the skilled technique you treated it made me to weep with delight. I’m just thankful for the help and in addition hope you are aware of a powerful job you are always carrying out instructing others through the use of your site. Probably you have never met any of us.

    回复 2019年10月16日
  11. air yeezy

    I want to voice my affection for your kindness in support of folks who really need help with this important theme. Your special commitment to passing the solution around had become rather beneficial and have all the time made women much like me to attain their goals. Your own interesting help denotes a lot to me and even more to my colleagues. Warm regards; from each one of us.

    回复 2019年10月16日
  12. adidas yeezy

    I not to mention my friends came reviewing the great advice from the blog while then developed an awful feeling I never thanked the site owner for those techniques. Most of the boys became for this reason passionate to learn all of them and have in effect very much been having fun with them. Appreciate your turning out to be considerably thoughtful and for figuring out some notable resources most people are really wanting to discover. My sincere regret for not expressing appreciation to you sooner.

    回复 2019年10月15日
  13. moncler jacket

    I must convey my gratitude for your kind-heartedness for persons who require assistance with this particular subject matter. Your special commitment to passing the message all over had become astonishingly interesting and has really empowered employees like me to realize their endeavors. Your new invaluable guideline signifies so much a person like me and somewhat more to my peers. Warm regards; from each one of us.

    回复 2019年10月14日
  14. kd 11

    My wife and i got so satisfied that Emmanuel managed to round up his inquiry via the precious recommendations he got when using the weblog. It’s not at all simplistic to just continually be freely giving facts which some other people could have been selling. So we do understand we have the writer to be grateful to because of that. All of the illustrations you made, the straightforward website menu, the friendships you will make it possible to promote – it’s mostly extraordinary, and it’s really aiding our son and the family reason why the topic is cool, which is pretty vital. Thanks for the whole lot!

    回复 2019年10月13日
  15. nike shox for women

    I wanted to compose you one little bit of note so as to say thanks a lot as before for your stunning concepts you have documented on this site. This is quite strangely generous with you to supply unhampered exactly what many individuals could have distributed for an e book to end up making some money for their own end, most notably given that you might well have tried it in case you decided. These secrets additionally served to become a great way to know that most people have the identical eagerness just like mine to know the truth whole lot more concerning this problem. I’m sure there are several more pleasant periods up front for people who check out your website.

    回复 2019年10月12日
  16. valentino

    I actually wanted to write a small comment to be able to express gratitude to you for all the great ideas you are placing at this site. My particularly long internet lookup has at the end been compensated with sensible ideas to go over with my two friends. I would believe that many of us site visitors are very endowed to exist in a perfect website with very many lovely professionals with good strategies. I feel extremely privileged to have encountered your webpages and look forward to some more cool moments reading here. Thanks again for all the details.

    回复 2019年10月11日
  17. air max 97

    I want to express some thanks to this writer for rescuing me from this type of predicament. Just after browsing throughout the search engines and meeting notions which were not pleasant, I figured my entire life was gone. Living minus the approaches to the issues you’ve sorted out through this article content is a crucial case, as well as the ones that would have adversely damaged my career if I hadn’t noticed your site. The knowledge and kindness in dealing with the whole lot was vital. I am not sure what I would’ve done if I hadn’t come upon such a subject like this. I am able to now relish my future. Thanks a lot very much for your impressive and results-oriented guide. I will not be reluctant to refer your web site to any person who should get counselling on this area.

    回复 2019年10月10日
  18. golden goose sneakers

    My husband and i ended up being now fortunate when John could deal with his investigations by way of the precious recommendations he came across in your web site. It is now and again perplexing just to always be freely giving hints which a number of people might have been trying to sell. And we also understand we now have the blog owner to thank for this. Most of the explanations you made, the simple web site menu, the relationships you can make it easier to instill – it’s everything overwhelming, and it’s really facilitating our son in addition to us know that the theme is brilliant, and that’s particularly vital. Thank you for the whole lot!

    回复 2019年10月9日
  19. timberland boots

    Thanks a lot for providing individuals with a very wonderful possiblity to read in detail from this website. It really is very pleasing and as well , stuffed with a lot of fun for me personally and my office acquaintances to visit the blog particularly thrice in one week to find out the newest guidance you will have. And lastly, I’m so usually satisfied with your stunning tactics served by you. Selected 3 facts in this post are without a doubt the most beneficial we have ever had.

    回复 2019年10月8日
  20. balenciaga

    My wife and i got very more than happy that Michael managed to carry out his inquiry from your ideas he had from your blog. It is now and again perplexing to simply always be releasing concepts which people could have been selling. And now we already know we now have the writer to thank for that. The type of explanations you have made, the easy site menu, the relationships you can make it easier to foster – it’s got most exceptional, and it’s making our son and our family reason why this concept is amusing, which is tremendously important. Thanks for the whole lot!

    回复 2019年10月8日