zoukankan      html  css  js  c++  java
  • git第一篇---基本命令

    摘要:

    (1)用git而不是svn。分布式而不是集中式

    (2)名词解释

    origin是父目录的意思,master是 一个特殊的分支而已。具体参看做最下边:


    1.创建仓库

    mkdir git

    cd git  ——创建/home/XXX/git空目录

    2.通过git init命令把这个目录变成Git可以管理的仓库:

    git init ——初始化Git仓库

    3.用命令git add告诉Git,把文件添加到仓库(实际上就是把文件修改添加到暂存区)

    git add filename

    4.用命令git commit告诉Git,把文件提交到仓库(实际上就是把暂存区的所有内容提交到当前分支)

    git commit -m "有意义的附加说明"

    5.随时掌握工作区的状态

    git status

    6.查看文件被修改的内容

    git diff

    7.查看代码的历史版本号

    git log

    git log --pretty=oneline   ——要求版本信息只能在一行中显示

    8.HEAD指向的版本就是当前版本,因此,Git允许我们在版本的历史之间穿梭

    git reset --hard commit_id

    或git reset --hard HEAD^(HEAD^^等等)

    9.查看命令历史,以便确定要回到未来的哪个版本

    git reflog

    10.弄明白Git的工作区(当前分区)和暂存区

    11.理解Git是如何跟踪修改的,每次修改,如果不add到暂存区,那就不会加入到commit中

    12.撤销修改

    命令git checkout -- filename意思就是,把filename文件在工作区的修改全部撤销,这里有两种情况:

    一种是filename自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;

    一种是filename已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。

    总之,就是让这个文件回到最近一次git commit或git add时的状态。

    场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file

    git checkout其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。

    场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD file,就回到了场景1,第二步按场景1操作。

    场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,版本回退,不过前提是没有推送到远程库。

     13.删除文件

    命令git rm用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容。

    14.将本地仓库与github仓库关联起来

    往里面添加文件:

    1 touch README.md
    2 git init
    3 git add README.md
    4 git commit -m "first commit"
    5 git remote add origin git@github.com:sysublackbear/Learmgitfirst.git
    6 git push -u origin master

    将本地仓库同步github仓库:

    1 git remote add origin git@github.com:sysublackbear/Learmgitfirst.git
    2 git push -u origin master

    然后,从现在起,只要本地作了提交,就可以通过命令:

    1 git push origin master

    把本地master分支的最新修改推送至GitHub

    15.多人协作一个项目的时候,我们每个人可以通过从远程仓库克隆一份来作为己用。

    1 git  clone git@github,com:sysublackbear/XXXX.git

    16.创建分支并且切换到分支

    1 git checkout -b dev
    2 Switched to a new branch 'dev'

    等价于:

    1 git branch dev
    2 git checkout dev
    3 Switched to branch 'dev'

    查看分支:

    1 git branch

    将次分支合并到主分支上面:

    1 git merge dev

    删除分支:

    1 git branch -d dev
    2 Deleted branch dev (was fec145a).

    17.解决冲突

    当Git无法自动合并分支时,就必须首先解决冲突。解决冲突后,再提交,合并完成。

    git log --graph命令可以看到分支合并图。

    18.Bug修复

    修复bug时,我们会通过创建新的bug分支进行修复,然后合并,最后删除;

    当手头工作没有完成时,先把工作现场git stash一下,然后去修复bug,修复后,再git stash pop,回到工作现场

    19.开发新功能

    开发一个新功能,最好新建一个分支;

    如果要丢弃一个没有被合并过的分支,可以通过git branch -D name强行删除。

    20.参与开源项目先要克隆一份到本地

    1 git clone git@github.com:michaelliao/bootstrap.git
    
    
    
    
    转自:http://www.cnblogs.com/sysu-blackbear/p/3463475.html
    
    


    关于git的操作命令 

    1)赋权限

    进入目录 cd .ssh/
    命令:  ssh-keygen
    获取key:  vim id_rsa.pub

    2)
    安装   apt-get install git-core
    获得   git clone git://url
    访问   http://url/gitweb


    3)纳入版本控制:
    git add *.txt     //添加指定文件
    git add README    //添加单个文件
    git add .         //添加所有文件包括子目录,但不包括空目录


    4)提交:
    git commit -m “no1”             //全部提交
    git commit -m “no1” someFile    //提交指定文件
    git commit -C HEAD -a —amend    //复用HEAD留言,增补提交(修改小错误,而不增加提交记录,掩盖自己的小马虎)

    -m “提交的说明”
    -a 动把所有已经跟踪过的文件暂存,并提交.(工作目录中修改过的文件都提交到版本库,不需一个一个手动add了)
    —amend 增补提交
    -C 复用指定提交的提交留言
    -c 打开编辑器在已有的提交基础上编辑修改


    5)查看提交历史:
    git log
    这时“j”向下浏览,“k”向上浏览,“q”退出


    6)问责:查明谁修改了代码
    git blame hello.html            //查看修改过文件的人
    git blame -L 12,+10 hello.html //12到22行  用"-L"参数在命令(blame)中指定开始和结束行:
    blame还可以跟踪内容复制,文件复制


    7)撤销缓存区的修改(没有commit的)

    git checkout head 文件名 //撤销暂存区的修改 
    git checkout head readme.txt todo.txt
    git checkout head *.txt
    git checkout head . //撤销所有

    8)反转提交:

    git revert HEAD //创建一个反向的新提交抵消原来的提交改动
    如果需要反转多个,必须从最后的开始反转, 加 -n可以不马上提交,之后一起提交。
    git revert -n HEAD
    git revert -n 54efhds
    git commit -m “revert head and 54efhds”


    9)复位:还没有commit,让工作目录回到上次提交时的状态

    git reset —hard HEAD //所有未提交的内容清空,这会让"git diff" 和"git diff —cached"命令的显示法都变为空
    git reset —soft HEAD //复位版本库,暂存差异,便于提交中发现错误需要更改时有用(例如私人密码放到里边了)


    10)分支:

    在当前分支末梢建立分支:

    git branch RB_1.0(建立分支不会自动切换过去)

    切换分支:

    git checkout RB_1.0(切换到RB_1.0分支)

    创建并切换分支:

    git checkout -b RB_1.0(简化上边2步操作)

    删除分支:

    git branch -d RB_1.0

    基于某次提交、分支或标签创建新分支:

    git branch RB_1.0 master
    git branch RB_1.0 6fe57de0
    git branch Rb_1.01 1.0

    查看分支:
    git branch -r //显示远程分支
    git branch -a //列出所有分支

    分支重命名:

    git branch -m master mymaster
    -M 大写M会覆盖同名的分支

    合并分支:

    直接合并:
    git merge 想合并到当前分支的源分支名
    git merge —no-commit 分支 //合并但不提交

    压合合并:将分支压合成一条commit记录,并合并过来
    git merge —squash 某bug分支
    git commit -m “修复某bug”

    拣选合并:只合并一个提交
    git cherry-pick 321d76f
    如果需要连续拣选,就需要加 -n参数
    然后再git commit ,但不要加-m参数,编辑器就会使用刚拣选的提交留言作为现在的留言。



    11)标签Tag:

    查看标签:

    git tag

    创建标签:

    git tag 1.0 //在当前分支最后一次提交创建标签
    git tag 1.0 RB_1.0 //基于RB_1.0分支的最新踢脚创建标签
    git tag 1.0 ae468d8kt //为某次提交创建标签

    检出标签:

    git checkout 1.0 //检出标签与检出分支一样操作,但检出标签后用git branch查看本地分支会发现你现在不再任何分支上
    这时你不应该修改,而应该立即基于此标签创建一个分支
    git checkout -b from-1.0



    12)变基:

    1)git rebase RB_1.01 //也许修改过一个bug,希望新版本变基到RB_1.01分支上
    2)手动解决冲突 //如果解决不了直接git rebase -skip或-abort来跳过特定提交或完全放弃变基
    3)git add xxx.html //冲突解决
    4)git rebase —continue

    //—onto参数可以改写历史抹掉中间的参数,将倒数第一个参数变基到倒数第3个参数,为防止出错建议在试验性分支上先试验。

    rebase -i 可以排序历史记录,多个提交合并为1个,一个提交分解成多个提交 ,
    详见版本控制之道p86 ,需要编辑器支持,windows记事本不行




    13)远程相关:

    git clone git://github.com/schacon/grit.git //从现有仓库克隆
    git clone git://github.com/schacon/grit.git mygrit //换名,唯一区别就是新建的目录成了mygrit,其他都一样

    添加远程仓库:

    git remote add pb git://github.com/paulboone/ticgit.git
    clone会默认添加origin仓库,如果原本用git init创建的版本库,后来又想提交到远程版本库,就可以用下边的办法
    git remote add origin git@example.com:/xxxxxx

    查看远程分支:

    git remote -v //查看远程仓库,默认clone后,应该有一个origin仓库,-v显示对应的clone地址
    git remote show origin //查看远程仓库信息

    远程仓库重命名和删除:

    git remote rename pb paul
    git remote rm paul

    获取数据:
    现在pb/master可以在本地访问了,你可以合并到自己的某个分支,或者切换到这个分支看看有什么有趣的更新

    git pull 抓取数据合并到工作目录中当前分支

    推送数据:

    git push [remote-name] [branch-name] //默认为 git push origin master

    git push origin serverfix //推送分支,其实是下边一句的简化,提取我的 serverfix 并更新到远程仓库的 serverfix

    git push origin serverfix:serferfix

    git push origin :serverfix //这个语法用于删除,只要把分号前留空



    14)其他:

    git gc //垃圾回收,每隔一段时间例如一个月运行一次可以减少磁盘占用空间。
    git reflog //最后的保障,列出误删的东东
    git bisect //二分查找,版本控制之道p124页,略

    归档版本库,导出压缩包:

    git archive —format=格式 —prefix=目录/ 版本>压缩包.zip
    git archive —format=zip head>test.zip
    git archive —format=tar —prefix=mysite-1.0/ 1.0 | gzip>mysite-1.0.tar.gz
    git archive —format=zip —prefix=mysite-1.0/ 1.0 >mysie-1.0.zip











    通过git pull更新仓库,使用git init-db初始化自己的仓库。


    commit:
     
    git commit -a -e        提交全部修改文件,并调用vim编辑提交日志。
    git reset HEAD^ or
    git reset HEAD~1        撤销最后一次提交。
    git reset --hard HEAD^  撤销最后一次提交并清除本地修改。
    git reset SHA1          回到SHA1对应的提交状态。
     
     
    add/delete/ls:
     
    git add -a              添加所有文件。除了.gitignore文件中的文件。
    git rm file             从git仓库中删除文件。
    git commit              添加或是删除后要提交。
     
    git ls-files -m         显示修改过的文件。
    git ls-files            显示所有仓库中的文件。


    這些事情都可以先在本地開 local branch 做,而不需要立即 Push 分享給別人。

    git branch name       建立本地 local branch
    git branch -m old_name new_name    改名字 (如果有同名會失敗,改用 -M 可以強制覆蓋)
    git branch           列出目前有那些 branch 以及目前在那個 branch
    git checkout name      切換 branch (注意到如果你有檔案修改了卻還沒 commit,會不能切換 branch,解法稍後會談)
    git checkout -b name       本地建立 branch 並立即 checkout 切換過去
    git branch -d  name       刪除 local branch

    git merge name     合併另一個 branch,若沒有 conflict 衝突會直接 commit。若需要解決衝突則會再多一個 commit。
    git merge --squash <branch_name> 將另一個 branch 的 commit 合併為一筆,特別適合需要做實驗的 fixes bug 或 new feature,最後只留結果。合併完不會幫你先 commit。
    git cherry-pick 321d76f 只合併特定其中一個 commit。如果要合併多個,可以加上 -n 指令就不會先幫你 commit,這樣可以多 pick幾個要合併的 commit,最後再 git commit 即可。


    查看 分支版本

    git branch -a


    除了master之外,我们还可以随便创建分支,然后push到服务器上去。

    $ git add .
    $ git commit -m ""
    $ git pull origin camp

    $: git push origin camp

    远程分支和本地分支需要区分,所以,在从服务器上拉取特定分支的时候,需要指定本地分支名字。

    $: git branch product origin/product

    更新分支到本地
    $: git pull origin camp
    转自:http://blog.163.com/zhulp0372@yeah/blog/static/115894479201241545917697/


    Remote Branches

    Remote branches are references (pointers) to the state of branches in your remote repositories. They’re local branches that you can’t move; they’re moved automatically for you whenever you do any network communication. Remote branches act as bookmarks to remind you where the branches on your remote repositories were the last time you connected to them.

    They take the form (remote)/(branch). For instance, if you wanted to see what themaster branch on your origin remote looked like as of the last time you communicated with it, you would check the origin/master branch. If you were working on an issue with a partner and they pushed up an iss53 branch, you might have your own local iss53 branch; but the branch on the server would point to the commit at origin/iss53.

    This may be a bit confusing, so let’s look at an example. Let’s say you have a Git server on your network at git.ourcompany.com. If you clone from this, Git’s clone command automatically names it origin for you, pulls down all its data, creates a pointer to where its master branch is, and names it origin/master locally. Git also gives you your own local master branch starting at the same place as origin’s master branch, so you have something to work from.

    “origin” is not special

    Just like the branch name “master” does not have any special meaning in Git, neither does “origin”. While “master” is the default name for a starting branch when you run git init which is the only reason it’s widely used, “origin” is the default name for a remote when you run git clone. If you run git clone -o booyah instead, then you will have booyah/master as your default remote branch.


  • 相关阅读:
    系统测试分类与常见的系统测试方法
    测试级别
    测试对象介绍
    软件测试基本介绍
    课外作业1:将一个double类型的小数,按照四舍五入保留两位小数
    将一个double类型的小数,按照四舍五入保留两位小数.
    将一个double类型的小数,按照四舍五入保留两位小数
    安装JDK1.8以及配置环境变量的步骤
    C# .Net实现URL绝对路径和相对路径之间互相转换
    64 位win 7或windows 8下的visual studio不能连接Oracle数据库调试网站的问题
  • 原文地址:https://www.cnblogs.com/catkins/p/5270445.html
Copyright © 2011-2022 走看看