zoukankan      html  css  js  c++  java
  • git 使用

    1、git 错误 fatal: Not a valid object name: 'master'.

    现在一开始的时候建立一个新的分支dev进行开发,执行 Git branch dev,出现错误 :fatal: Not a valid object name: 'master'.

    原来,Git要先commit一次之后才会真正建立master分支,此时才可以建立其它分支

    D:pythonWorkspacemain_code>git status
    On branch master

    No commits yet

    Changes to be committed:
    (use "git rm --cached <file>..." to unstage)

    new file: test.txt


    #报错,无法创建新的分支!!!!!!!!!!!!!!!
    D:pythonWorkspacemain_code>git branch branch_20190808
    fatal: Not a valid object name: 'master'.

    #提交到master分支
    D:pythonWorkspacemain_code>git add .


    D:pythonWorkspacemain_code>git commit .
    [master (root-commit) eda51c8] new file: test.txt
    1 file changed, 0 insertions(+), 0 deletions(-)
    create mode 100644 test.txt

    D:pythonWorkspacemain_code>git branch
    * master

    #创建分支
    D:pythonWorkspacemain_code>git branch branch_20190808

    D:pythonWorkspacemain_code>git branch
    branch_20190808
    * master

    #切换分支
    D:pythonWorkspacemain_code>git checkout branch_20190808
    Switched to branch 'branch_20190808'

    D:pythonWorkspacemain_code>git status
    On branch branch_20190808
    nothing to commit, working tree clean


    2、服务器创建裸仓,只有只读功能
    [root@iZ2ze23w0nyvm3rd82zgqlZ webstock]# git init --bare myserver.init
    Initialized empty Git repository in /root/webstock/myserver.init/

    [root@iZ2ze23w0nyvm3rd82zgqlZ webstock]# ls
    myserver.init

    [root@iZ2ze23w0nyvm3rd82zgqlZ myserver.init]# ls
    branches config description HEAD hooks info objects refs


    3、git clone

    3.1客户端克隆远程全部分支的数据
    > git clone root@39.106.26.41:/root/webstock/myserver.init


    克隆完成后,本地main_code26下只有.git文件夹,没有其他数据;
    > git remote -v
    origin root@39.106.26.41:/root/webstock/myserver.init (fetch)
    origin root@39.106.26.41:/root/webstock/myserver.init (push)


    切换到某一个远程分支,就可以看到该分支对应的数据了
    > git checkout branch_0801
    Switched to branch 'branch_0801'
    Your branch is up to date with 'origin/branch_0801'.

    3.2 客户端git拉取远程分支branch_0801的数据, 直接可以看到.git和远程分支branch_0801的数据
    > git clone -b branch_0801 root@39.106.26.41:/root/webstock/myserver.init
    Cloning into 'myserver.init'...
    root@39.106.26.41's password:
    remote: Counting objects: 14, done.
    remote: Compressing objects: 100% (7/7), done.
    remote: Total 14 (delta 0), reused 0 (delta 0)
    Receiving objects: 100% (14/14), 1.15 KiB | 195.00 KiB/s, done.

    4.git merge合并时遇上refusing to merge unrelated histories的解决方案
    如果git merge合并的时候出现refusing to merge unrelated histories的错误,原因是两个仓库不同而导致的,需要在后面加上--allow-unrelated-histories进行允许合并,即可解决问题

    #fetch 远程分支branch_0801到本地temp分支上,如果temp分支不在,会自动创建temp分支
    D:pythonWorkspacemain_code27>git fetch origin branch_0801:temp
    root@39.106.26.41's password:
    From 39.106.26.41:/root/webstock/myserver.init
    * [new branch] branch_0801 -> temp

    D:pythonWorkspacemain_code27>
    D:pythonWorkspacemain_code27>git branch
    master
    temp
    * test_branch

    #merge提示历史信息不一致
    D:pythonWorkspacemain_code27>git merge temp
    fatal: refusing to merge unrelated histories

    #merge成功
    D:pythonWorkspacemain_code27>git merge temp --allow-unrelated-histories
    Merge made by the 'recursive' strategy.
    1.c | 3 +++
    test1.txt | 1 +
    2 files changed, 4 insertions(+)
    create mode 100644 1.c
    create mode 100644 test1.txt

    D:pythonWorkspacemain_code27>git checkout temp
    Switched to branch 'temp'

    D:pythonWorkspacemain_code27>

    5、git branch new_branch创建新分支,新分支的内容是基于当前的分支的,等于是新分支是在当前分支的基础上复制过来的;

    6、在分支上如果做了修改,再执行fetch和merge也无法更新到远程原本的值了,必须使用 git reset --hard "HEAD^"才能恢复,
    所以,建议不要在原本的分支上直接修改,应该在原本分支上创建一个新分支,基于创建的新分支上进行修改,修改完成了再merge回当前分支;

    7、git init后默认了一个master分支,需要commit后master才生效,然后才可以创建其他分支

    #报错,无法创建新的分支!!!!!!!!!!!!!!!
    D:pythonWorkspacemain_code>git branch branch_20190808
    fatal: Not a valid object name: 'master'.

    #提交到master分支
    D:pythonWorkspacemain_code>git add .


    D:pythonWorkspacemain_code>git commit .
    [master (root-commit) eda51c8] new file: test.txt
    1 file changed, 0 insertions(+), 0 deletions(-)
    create mode 100644 test.txt

    D:pythonWorkspacemain_code>git branch
    * master

    #创建分支
    D:pythonWorkspacemain_code>git branch branch_20190808

    D:pythonWorkspacemain_code>git branch
    branch_20190808
    * master

    #切换分支
    D:pythonWorkspacemain_code>git checkout branch_20190808
    Switched to branch 'branch_20190808'

    D:pythonWorkspacemain_code>git status
    On branch branch_20190808
    nothing to commit, working tree clean

    8、git基本使用

    初始化git
    D:pythonWorkspaceAdvocateCar>git init
    Initialized empty Git repository in D:/pythonWorkspace/AdvocateCar/.git/

    D:pythonWorkspaceAdvocateCar>git config --global user.name "xiao"

    D:pythonWorkspaceAdvocateCar>git config --global user.email "xiao@qq.com"

    D:pythonWorkspaceAdvocateCar>git remote add rtAdvocateCar root@39.106.26.41:/root/webstock/AdvocateCar.init


    #创建django工程

    D:pythonWorkspace>D:pythonWorkspacedjango_envScriptsdjango-admin.exe startproject AdvocateCar

    D:pythonWorkspaceAdvocateCar>dir
    驱动器 D 中的卷是 Data
    卷的序列号是 E0B9-ECA2

    D:pythonWorkspaceAdvocateCar 的目录

    2019/08/08 17:28 <DIR> .
    2019/08/08 17:28 <DIR> ..
    2019/08/08 17:27 <DIR> .idea
    2019/08/08 17:26 <DIR> AdvocateCar
    2019/08/08 17:26 652 manage.py
    1 个文件 652 字节
    4 个目录 432,316,743,680 可用字节


    #push到远程仓库
    D:pythonWorkspaceAdvocateCar>git branch b0808

    D:pythonWorkspaceAdvocateCar>git checkout b0808
    Switched to branch 'b0808'

    D:pythonWorkspaceAdvocateCar>git push rtAdvocateCar b0808:rt_b0808
    root@39.106.26.41's password:
    Enumerating objects: 13, done.
    Counting objects: 100% (13/13), done.
    Delta compression using up to 8 threads
    Compressing objects: 100% (12/12), done.
    Writing objects: 100% (13/13), 5.50 KiB | 1.83 MiB/s, done.
    Total 13 (delta 0), reused 0 (delta 0)
    To 39.106.26.41:/root/webstock/AdvocateCar.init
    * [new branch] b0808 -> rt_b0808

    D:pythonWorkspaceAdvocateCar>

    9. git提交时”warning: LF will be replaced by CRLF“提示

     

    今天把项目做完之后,然后用Git准备提交代码的时候,遇到warning: LF will be replaced by CRLF警告。
    当时在网上查了资料,发现很多的解决办法都是:修改git全局配置,禁止git自动将LF转化成CRLF。命令是:git config --global core.autocrlf false.

    LF和CRLF的真相是什么

    1.LF和CRLF都是换行符,在各操作系统下,换行符是不一样的,Linux/UNIX下是LF,而Windows下是CRLF,早期的MAC OS是CR,后来的OS X在更换内核后和UNIX一样也是LF.

     

    10、git clone 指定分支的内容

     

      使用Git下载指定分支命令为:git clone -b 分支名仓库地址

      使用Git下载v.2.8.1分支代码,使用命令:git clone -b v2.8.1 https://git.oschina.net/oschina/android-app.git

  • 相关阅读:
    华为机试题01背包问题
    丑数
    动态规划(1)
    Linux 后台启动 Redis
    redis.exceptions.ResponseError: MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk.
    SQLServer从渣仔到小白
    cmder 增强型命令行工具
    总结在部署分布式爬虫环境过程中常见的若干问题
    【pymongo.errors】Cursor not found
    浅析scrapy与scrapy_redis区别
  • 原文地址:https://www.cnblogs.com/harryTree/p/11320335.html
Copyright © 2011-2022 走看看