GIT学习

福尔摩斯对我笑 2018-06-13 1450 次 143条

  初次使用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
未来博客 博主大人
到了这个年纪,不应该再彷徨!

发表评论



 


已有 143 条评论 1


  1. Kiacycle 回复 2020年2月20日
  2. kyrie 5

    A lot of thanks for all of your labor on this site. Betty delights in participating in internet research and it’s simple to grasp why. Most people hear all regarding the compelling way you create advantageous secrets through your website and as well as welcome response from website visitors about this idea then our own daughter is always being taught so much. Take pleasure in the rest of the new year. You are carrying out a really great job.

    回复 2020年2月18日
  3. cursos facebook ads iniciantes

    Por exemplo, o e-mail marketing pode ser uma grande oportunidade para se manter em contato com seus clientes. Confira este artigo para encontrar maneiras mais poderosas de realmente tornar seus métodos mais sensíveis às necessidades de sua base de clientes.

    回复 2020年2月17日
  4. ig

    Hey there would you mind sharing which blog platform you’re working with?

    I’m looking to start my own blog in the near future but I’m having a
    hard time choosing between BlogEngine/Wordpress/B2evolution and Drupal.

    The reason I ask is because your design and style seems different then most blogs and I’m looking for something
    completely unique. P.S Sorry for getting
    off-topic but I had to ask!

    回复 2020年2月14日
  5. air jordan

    My husband and i got very thrilled that Louis could finish off his investigation via the ideas he got through the web site. It is now and again perplexing just to find yourself releasing procedures which often most people could have been selling. And we consider we have got the blog owner to be grateful to because of that. The type of illustrations you’ve made, the easy web site menu, the friendships you can aid to create – it’s got most incredible, and it’s really letting our son and our family reckon that the subject matter is satisfying, and that is especially mandatory. Thanks for all!

    回复 2020年2月13日
  6. sns.lilyenglish.com

    I like the valuable info you provide in your articles.

    I will bookmark your weblog and check again here frequently.
    I am quite sure I will learn a lot of new stuff right here!
    Best of luck for the next!

    回复 2020年2月12日
  7. Micaela

    Write more, thats all I have to say. Literally, it seems
    as though you relied on the video to make your point.
    You clearly know what youre talking about, why waste your intelligence on just posting videos to your weblog
    when you could be giving us something informative to read?

    回复 2020年2月12日
  8. Lisacycle 回复 2020年2月12日
  9. Kimcycle 回复 2020年2月11日
  10. supreme clothing

    I would like to express thanks to you just for bailing me out of this type of predicament. After browsing throughout the world-wide-web and obtaining ideas that were not pleasant, I believed my entire life was done. Being alive minus the approaches to the issues you’ve fixed by means of your good post is a critical case, as well as the ones which may have badly affected my career if I hadn’t discovered your web site. Your good understanding and kindness in handling all the pieces was priceless. I don’t know what I would have done if I hadn’t encountered such a stuff like this. I am able to at this point look ahead to my future. Thanks a lot very much for your reliable and sensible help. I will not be reluctant to propose your web blog to any individual who will need direction on this area.

    回复 2020年2月9日
  11. Kiacycle 回复 2020年2月8日
  12. golden goose sneakers

    Thank you a lot for giving everyone remarkably marvellous chance to read critical reviews from this web site. It is usually so amazing and as well , jam-packed with a great time for me and my office friends to visit the blog nearly three times per week to find out the latest guides you have. Of course, I am usually satisfied concerning the impressive tactics you serve. Selected 1 facts on this page are honestly the most suitable I’ve ever had.

    回复 2020年2月5日
  13. Wimcycle 回复 2020年2月4日
  14. nmd

    Needed to compose you this little note to help thank you over again for the spectacular concepts you’ve documented at this time. It has been quite extremely open-handed with you in giving freely all that a lot of people would have offered as an e book to help make some profit for themselves, notably since you might have tried it in case you wanted. These tips as well acted to become good way to comprehend most people have a similar passion the same as mine to realize way more concerning this matter. I believe there are many more fun sessions up front for individuals that browse through your site.

    回复 2020年1月29日
  15. Lisacycle 回复 2020年1月28日
  16. Janecycle 回复 2020年1月28日
  17. Kimcycle 回复 2020年1月28日
  18. balenciaga shoes

    I am only writing to make you know what a cool experience my daughter found browsing your web page. She mastered numerous issues, including what it is like to possess a wonderful teaching mindset to have the others quite simply completely grasp several hard to do subject matter. You truly surpassed our expected results. Many thanks for distributing the warm and friendly, dependable, informative and in addition cool guidance on your topic to Lizeth.

    回复 2020年1月26日
  19. Kiacycle 回复 2020年1月22日
  20. MatirrilD

    Peut Acheter Viagra Sans Ordonnance Cialis 8 Comprimidos Buy Cialis Drugstore Hong Kong Amoxicillin Tooth

    回复 2020年1月16日
  21. converse shoes

    I must show my admiration for your kind-heartedness for folks that should have help on this important matter. Your real commitment to getting the solution all over has been extremely informative and has truly empowered many people like me to realize their ambitions. Your new important information signifies this much to me and somewhat more to my office colleagues. With thanks; from each one of us.

    回复 2020年1月14日
  22. off white clothing

    I am just commenting to let you know of the magnificent experience my wife’s daughter obtained checking your web page. She learned such a lot of issues, with the inclusion of what it’s like to have a great coaching spirit to have other individuals without difficulty learn about specified complex subject matter. You truly did more than readers’ desires. Thanks for giving those powerful, trustworthy, revealing and even fun tips about this topic to Janet.

    回复 2020年1月11日
  23. golden goose sneakers

    I and also my pals came following the great tips and hints from your web site then then I got an awful feeling I had not expressed respect to the blog owner for those strategies. All the women came for that reason very interested to see them and already have quite simply been taking advantage of these things. We appreciate you simply being simply thoughtful and also for settling on this form of fabulous resources most people are really eager to know about. Our own honest regret for not expressing gratitude to you earlier.

    回复 2020年1月7日
  24. longchamp handbags

    I’m also commenting to make you understand of the superb experience our daughter experienced reading your site. She realized a lot of things, most notably how it is like to have a very effective teaching nature to get other people with ease gain knowledge of several tricky topics. You truly surpassed our own expectations. Many thanks for showing the productive, dependable, revealing not to mention fun tips about your topic to Kate.

    回复 2020年1月4日
  25. christian louboutin

    I simply needed to appreciate you again. I am not sure the things I would have implemented in the absence of these creative concepts revealed by you over my topic. Entirely was a horrifying concern in my circumstances, but spending time with your well-written fashion you managed that made me to leap for contentment. I’m happier for this support and in addition have high hopes you realize what a great job that you are doing teaching others with the aid of your websites. Probably you’ve never met any of us.

    回复 2019年12月31日
  26. yeezy shoes

    I actually wanted to type a small remark in order to express gratitude to you for the superb hints you are posting here. My considerable internet search has at the end been recognized with sensible details to go over with my company. I would declare that many of us website visitors actually are definitely fortunate to dwell in a good place with very many special people with insightful plans. I feel quite privileged to have used the web page and look forward to some more thrilling times reading here. Thank you once more for everything.

    回复 2019年12月23日
  27. jordan 11

    I am only writing to let you be aware of of the fabulous experience our princess found checking your webblog. She came to understand a lot of issues, including what it’s like to possess an incredible giving spirit to have other individuals completely know just exactly chosen extremely tough matters. You undoubtedly surpassed my expected results. Many thanks for rendering such warm and helpful, healthy, educational and even cool tips on the topic to Kate.

    回复 2019年12月19日
  28. jordans

    Hello! I just want to give a huge thumbs up for the great info you might have right here on this post. I might be coming back to your weblog for more soon.

    回复 2019年12月5日
  29. hermes birkin

    Spot on with this write-up, I truly assume this web site wants much more consideration. I抣l probably be once more to read much more, thanks for that info.

    回复 2019年12月2日
  30. jordan 12

    I found your blog web site on google and examine a number of of your early posts. Proceed to keep up the excellent operate. I simply further up your RSS feed to my MSN Information Reader. Seeking ahead to studying extra from you in a while!?

    回复 2019年11月29日
  31. golden goose

    I抎 must test with you here. Which isn’t something I often do! I get pleasure from reading a publish that will make individuals think. Additionally, thanks for permitting me to comment!

    回复 2019年11月26日
  32. canada goose

    Would you be fascinated by exchanging links?

    回复 2019年11月23日