Git 常用命令

来源:互联网 发布:淘宝联盟怎么注册 编辑:程序博客网 时间:2024/06/02 02:33

创建git全局配置

装载请注明出处: http://blog.csdn.net/xiaowu_zhu/article/details/78332164

git config --global user.name="xiaowujiang" git config --global user.email="xiaowu_zhu@126.com"

新建一个本地的仓库

  • 项目目录—-> git init

将代码托管到网上

  • 首先登陆到github或bitbucket
  • 创建一个项目
  • git clone //从网站上克隆

    https://github.com/xiaowu-zhu/cool-weather.githttps://xiaowu-zhu@bitbucket.org/xiaowu-zhu/cool-weather.git
  • cp -R /cool-weather/. . //将克隆的文件全部复制到项目的根目录

  • git add .

  • git commit -m “提交的内容”

  • git push origin master

将代码同步到本地

  • git pull origin master(没有git fetch安全)

  • git fetch

    • git fetch origin master:tmp
    • git diff tmp
    • git merge tmp

查看分支

  • git branch(本地的)
  • git branch -a (远程的)

新建分支

  • git checkout -b 分支名(新建分支)

删除分支

  • git branch -d tmp

切换分支

  • git checkout 分支名

修改提交的注释

  • git commit –amend

查看提交的log

  • git log
  • git reflog

撤销提交

git reset --hard HEAD(头信息git reflog中可查看)

强制到远程

git push -f

修改远程仓库

方法有三种:

  1. 修改命令

    git remote set-url origin [url]

    例如:

    Git remote set-url origin gitlab@gitlab.chumob.com:PHP/hasoffer.git
  2. 先删后加

    git remote rm origingit remote add origin [url]

    例如:

    git remote add origin https://xiaowu_zhu@bitbucket.org/xiaowu-zhu/working.git
  3. 直接修改config文件

常见问题

Updates were rejected because the tip of your current branch is behind

  • 方法1:

    • 强制push(一般不可取,这样会使远程修改丢失)

      git push -u origin master - f
  • 方法2:

    • push前,先将远程分支pull下来

      git pull origin mastergit push origin master
  • 方法3:

    • 若不想merge远程和本地修改,可以先创建新的分支

      git branch [name]
    • 然后push

      git push -u  origin [name]

更多信息请看: https://xiaowujiang.cn/blog/

原创粉丝点击