git 学习

来源:互联网 发布:淘宝追加好评长心吗 编辑:程序博客网 时间:2024/06/05 20:49

[github 上添加 SSH key]
    (1)创建一个 SSH key
        $ ssh-keygen -t rsa -C "your_email@example.com"
        代码参数含义:
        -t 指定密钥类型,默认是 rsa ,可以省略。
        -C 设置注释文字,比如邮箱。
        -f 指定密钥文件存储文件名。
    (2)测试ssh key
        ssh -T -p 443 git@gitlab.spriteapp.com.cn
[git帮助操作]
    git --help
    git help <command>
[git 初始化仓库]
    git init
    git clone https://github.com/anty-zhang/test.git
    git remote add origin https://github.com/anty-zhang/test.git
    git remote -v  # 查看远程仓库

    git add README.md
    git commit -m "first commit"
    git push -u origin master

    git add .gitignore  (忽略某些文件)比如:
        [andy@localhost reader-server]$ cat .gitignore
            *.pyc
            env
            .coverage
            .DS_Store
            .scrapy
            .tags
            logs
            *.pid
            bak
            *.txt

[显示信息类命令]
    git status -s
    git ls-files
    git log
    git log --pretty=oneline (显示一行)
    git log --stat   # 查看每次提交统计信息
    git log -p <file>   # 查看每次详细修改内容的diff
    git log -p -2       # 显示最近两次修改内容的diff
    git show    # 显示每次提交的内容
    git show $id
    git reflog (记录每一次的操作命令,可以用于版本回退)
    git reset --hard HEAD^  (回退到上一个版本)
    git reset --hard HEAD^^ (回退到前两个版本)
    git branch   --查看远程分支
    git branch -r   --查看远程分支有哪些用户
    git diff <branch1>..<branch2> # 在两个分支之间比较
    git diff --staged   # 比较暂存区和版本库差异  
    git diff --cached   # 比较暂存区和版本库差异  
    git diff --stat     # 仅仅比较统计信息

[提交类命令]
    git add xingzuo/ -A

[删除类命令]
    git rm <file>   # 从版本库中删除文件
    git rm <file>  --cached   # 从版本库中删除文件,但不删除文件

[git分支管理]
    (1)从master_branch或者develop_branch新建分支branch_name
    git checkout -b branch_name master_branch/develop_branch
    (2)将branch_name发布到master分支
    git checkout master
    git merge --no-ff -m 'comment' branch_name
    (3)删除branch_name分支
    git branch -d branch_name
    (4)查看分支合并图
    git log --graph --pretty=oneline --abbrev-commit

[git config设置]
    (1)git config的范围
    --system  会配置在/etc/gitconfig文件中
    --global 会写在~/.gitconfig文件中
    当前项目目录 .git/config文件
    后者会覆盖前者,如果变量名相同的话。


    (2)设置git默认编辑器
    git config --global core.editor emacs
    git config --global merge.tool vimdiff
    这时候,再用git config -e 就会打开emacs编辑器,并且emacs已经打开了当前项目的config文件。

    git config -e --global 会打开~/.gitconfig文件
    git config -e --system 会打开/etc/gitconfig文件
    (3)查看git设置
    git config --list
    如果查看global的设置,用下面的命令:
    git config --global --list
    (4)删除设置
    我们可以直接编辑文件,也可以用类似下面的命令
    git config--unset --global user.name
    将~/.gitconfig文件中的user.name删除了。


[git解决冲突的方法]
    git stash
    git stash list可以看到保存的信息
    git pull
    git stash pop stash@{0}
    手动合并冲突
    git push


[git commit撤销]
    git reset --hard <commit_id>
    根据–soft –mixed –hard,会对working tree和index和HEAD进行重置:
    git reset –mixed:此为默认方式,不带任何参数的git reset,即时这种方式,它回退到某个版本,只保留源码,回退commit和index信息
    git reset –soft:回退到某个版本,只回退了commit的信息,不会恢复到index  file一级。如果还要提交,直接commit即可(只是撤销了commit的提交记录,commit改动的代码仍然存在,很受用)
    git reset –hard:彻底回退到某个版本,本地的源码也会变为上一个版本的内容

[.git/config 备份]
    [andy@localhost reader-server]$ cat .git/config
    [core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
    [remote "origin"]
        url = git@gitlab.com.cn:reader/reader-server.git
        fetch = +refs/heads/*:refs/remotes/origin/*
    [branch "master"]
        remote = origin
        merge = refs/heads/master
    [branch "branch0.01"]
        remote = origin
        merge = refs/heads/branch0.01
    [remote "sec"]
        url = ssh://root@myhome.com.cn:2230/tmp/reader
        fetch = +refs/heads/*:refs/remotes/sec/*
0 0