git

作者: BingNa | 来源:发表于2018-06-13 23:59 被阅读0次

    git基本操作

    安装git

    sudo apt-get install git
    

    初始化仓库(repository)

    git init
    

    提交更改到暂存区

    git add 'filename'
    

    提交更改到版本库

    git commit -m 'commit describe'
    

    撤销更改

    # 未提交至暂存区
    git checkout -- 'filename' #未git add之前,撤销对‘filename’文件的修改
    # 已经有git add 提交至暂存区,未git commit
    git reset HEAD 'filename' # 撤销提交到暂存区的修改,重新放回工作区
    git checkout -- 'filename' 
    

    删除文件

    rm 'filename'
    git rm 'filename'
    git commit -m 'commit describe'
    

    误删找回

    git checkout -- ‘filename’ # 删除filename后未提交至版本库之前,恢复文件,但是只能找回文件在版本库中的状态。上次提交后到删除前的修改丢失。
    

    版本回退

    git reset --hard HEAD^ # 回退至上一个版本
    git reset --hard HEAD^^ # 回退至倒数第二个版本
    git reset --hard HEAD~100 # 回退至倒数第100个版本
    
    git reset --hard ‘commit id’ # 回退至某个’commit id‘的版本
    

    查看版本库状态

    git log
    git log --pretty=oneline # 简洁
    git log --graph --pretty=oneline --abbrv-commit
    

    查看命令操作记录

    git reflog
    

    远程放库

    创建SSH Key

    ssh-keygen -t rsa -C "youremail@example.com"
    

    将.ssh文件夹下的id_rsa.pub中的内容加入github的SSH Key

    关联本地仓库与远程仓库,git默认远程仓库名字是origin

    git remote add origin git@github.com:yourgithubname/repoName.git
    

    将本地所有内容推送至空的远程仓库

    git push -u origin master
    

    git push命令,实际上是把当前分支master推送到远程。

    由于远程库是空的,我们第一次推送master分支时,加上了'-u'参数,Git不但会把本地的master分支内容推送的远程的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。

    git push origin master
    

    把本地master分支推送至远程仓库

    从远程仓库clone一个本地库

    git clone git@github.com:gihubname/repoName.git
    

    关于分支的介绍

    创建分支

    $ git checkout -b dev # 创建dev分支,然后切换到dev分支
    

    git checkout命令加上-b参数表示创建并切换,相当于以下两条命令:

    $ git branch dev
    $ git checkout dev
    

    切换到另一个分支

    git checkout 'branchName'
    

    合并分支,合并branchName到当前分支

    git merge 'branchName'
    

    合并分支,并禁用fast forward,并保留合并这一操作的commit describe

    git merge --no-ff -m 'commit describe' 'branchName'
    

    删除分支

    git branch -d 'branchName'
    

    临时保存工作现场

    git stash
    

    恢复工作现场

    git stash apply # 不删除stash
    git stash pop # 恢复的同时,删除stash
    

    查看暂存的工作现场

    git stash list
    

    多次stash,恢复某个暂存的现场

    git stash apply stash@{0/1/...}
    

    查看远程仓库信息

    git remote
    git remote -V
    

    解决其他人的提交与自己提交有冲突的问题

    若没有建立本地仓库与远程仓库的链接,则首先建立链接

    git branch --set-upstream-to=origin/remoteBranchName localBranchName
    

    拉取最新的远端分支

    git pull
    

    此时,本地文件会提示错误,手动修改错误,并再次提交git commit,并推送 git push

    打标签

    为commit describe 打上标签,更加容易记住

    git tag 'tagName'
    

    多个远端仓库

    git remote add anotherOriginName git@git.coding.net:codingName/repoName.git
    

    相关文章

      网友评论

        本文标题:git

        本文链接:https://www.haomeiwen.com/subject/xnjseftx.html