温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Git基础入门(七)Git撤销操作和远程仓库管理

发布时间:2020-07-19 08:08:16 来源:网络 阅读:802 作者:红尘世间 栏目:软件技术

撤销操作:


注意:Git的有些撤消操作是不可逆的。 这是在使用Git的过程中,会因为操作失误而导致之前的工作丢失的少有的几个地方之一


取消暂存的文件


git add a.py b.py 

git status 

    On branch master

    Changes to be committed:

      (use "git reset HEAD <file>..." to unstage)           #提示如何撤销


    modified:   a.py

    modified:   b.py


git reset HEAD b.py                                         #取消暂存b.py

    Unstaged changes after reset:

    M b.py


git status 

    On branch master

    Changes to be committed:

      (use "git reset HEAD <file>..." to unstage)


    modified:   a.py


    Changes not staged for commit:

      (use "git add <file>..." to update what will be committed)

      (use "git checkout -- <file>..." to discard changes in working directory)         #提示可以撤销对文件的修改


    modified:   b.py




撤消对文件的修改

    git checkout b.py

    git status 

        On branch master

        Changes to be committed:

          (use "git reset HEAD <file>..." to unstage)


        modified:   a.py


git checkout -- [file] 是一个危险的命令,如果执行了这个命令你对那个文件做的任何修改都会消失





远程仓库:

    远程仓库是指托管在因特网或其他网络中的你的项目的版本库,远程仓库可以有多个,通常有些仓库对你只读,有些则可以读写

    管理远程仓库包括了解如何添加远程仓库、移除远程仓库、管理不同的远程分支并定义它们是否被跟踪等等







查看远程仓库

    git remote              #查看当前所有的远程仓库

        origin              #origin 是Git给你克隆的仓库服务器的默认名字


    -v选项,显示远程仓库的简写与其对应的URL

    git remote -v

        origin https://github.com/libgit2/libgit2 (fetch)

        origin https://github.com/libgit2/libgit2 (push)




添加远程仓库

git remote add <shortname> <url>            #添加一个新的远程Git仓库,同时指定一个简写


git remote add test https://github.com/huyuan1999/17-10-22.git          #添加远程仓库


git remote -v

    origin https://github.com/libgit2/libgit2 (fetch)

    origin https://github.com/libgit2/libgit2 (push)

    test https://github.com/huyuan1999/17-10-22.git (fetch)

    test https://github.com/huyuan1999/17-10-22.git (push)



现在可以在命令行中使用test来代替整个URL

    git fetch test          #拉取远程仓库中的信息(本地工作目录中没有的信息)




从远程仓库中抓取与拉取

    git fetch [remote-name]     #拉取远程仓库中的数据(不会自动合并分支)

    如果使用clone命令克隆了一个仓库,并将其添加为远程仓库默认以origin为简写。所以git fetch origin会抓取克隆后新推送的所有数据


    git pull [remote-name]      #自动的抓取然后合并远程分支到当前分支

    默认情况下git clone会自动设置本地master分支跟踪克隆的远程仓库master分支,运行git pull通常会从克隆的服务器上抓取数据并自动尝试合并到当前分支



推送到远程仓库

    git push [remote-name] [branch-name]            #推送指定分支到服务器中

    git push test master              #git默认使用github做为远程仓库服务器,如果想要推送到远程仓库则需要有对应的账号和密码




查看远程仓库


git remote show test

    * remote test                               #本地简写

      Fetch URL: https://github.com/huyuan1999/17-10-22.git

      Push  URL: https://github.com/huyuan1999/17-10-22.git

      HEAD branch: master                       #处于的分支                

      Remote branch:

        master tracked                          #掌握跟踪

      Local ref configured for 'git push':

        master pushes to master (up to date)




远程仓库的移除与重命名

    git remote rename test hu               #重命名

    git remote

        origin

        hu



    git remote rm hu                        #移除

    git remote

        origin


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI