总结使用git的一些心得

   这是我的第一篇随笔,主要记录自己的点点滴滴,留下足迹。希望有一天成为技术大牛!

  git教程推荐菜鸟教程http://www.runoob.com/git/git-tutorial.html  和廖雪峰教程https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/

操作git脑海中要记得三个概念:工作区、暂存区(.git/index)、版本库(.git)

git add .        工作区----->暂存区

git commit -m "comment"      暂存区---->版本库

 

 还有一些基本概念:

     tracked(已跟踪)   untracked(从未跟踪,没被add或commit过)   staged(进入暂存区)  unstaged(未进入暂存区)

    git  reset HEAD 撤销已经暂存的文件

    git checkout --file     取消文件修改,回到之前版本?有危险

   分支管理:

     git checkout -b (branchname)          创建新分支并立即切换到改分支下

    git branch (branchname)              创建分支

    git branch                                      查看分支

   git checkout (branch)                     切换分支

   git merge (branch)                          将(branch)合并到当前分支

   git branch -d  branchname    删除本地分支

 git branch -D  branchname    强行删除本地分支

 远程仓库:

  git remote add origin git@balalal.git     关联远程仓库

  git checkout -b feature/international origin/feature/international    (创建本地分支feature/international并立即切换到,关联远程分支origin/feature/international)

 git push -u origin master                         第一次推送到远程仓库

 

   指令很麻烦,而且容易出错,不过可以提高git操作熟练度。用webstorm操作git会方便很多,还不容易出错,推荐教程https://www.cnblogs.com/jinguangguo/p/4868152.html

   想到前几天把公司远程库删了个文件夹,我就老脸一红。

 版本回滚(亲测):

    git log                         查看日志

    git reset --hard HEAD  回退到上个版本

              git reset --hard commit_id  退到制定commit id 的sha码

 

更新:     工作分支:feature/international

git pull 时出现冲突,想着直接让远程分支覆盖本地代码,在网上搜索到如下办法:

git fetch --all //只是下载代码到本地,不进行合并操作
git reset --hard origin/master  //把HEAD指向最新下载的版本

出现了问题,因为最新下载的版本不是我工作的分支,所以
HEAD指向了另一个分支,导致表面上git还是在工作分支,实际上已经不是了。还好没有直接合并,否则会改变远程分支代码。
后来想着git checkout feature/international 切换到工作分支,可是出现如下问题:

  You are in 'detached HEAD' state. You can look around, make experimental
  changes and commit them, and you can discard any commits you make in this
  state without impacting any branches by performing another checkout.

  If you want to create a new branch to retain commits you create, you may
  do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

  HEAD is now at ...

detached HEAD state   游离状态   好吓人


解决办法:
切换到master分支, 删除本地工作分支,然后在创建相同名字分支再关联远程分支,ok了!

      圣光会赐予我胜利!加油!

转载于:https://www.cnblogs.com/da-da/p/8419130.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值