zoukankan      html  css  js  c++  java
  • git常用命令备忘

    1。 查看当前版本

    1 [root@CloudGame gatling]# git --version
    2 git version 2.6.0-rc1

    2。查看git的配置信息,(若在当前git本地仓库里,包含当前项目的信息)

     1 [root@CloudGame gatling]# git config -l
     2 user.name=chengsh
     3 user.email=shihu.cheng@xxxx.com
     4 push.default=simple
     5 core.repositoryformatversion=0
     6 core.filemode=true
     7 core.bare=false
     8 core.logallrefupdates=true
     9 remote.origin.url=https://github.com/gatling/gatling
    10 remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
    11 branch.2.1.X.remote=origin
    12 branch.2.1.X.merge=refs/heads/2.1.X

    这里,提供一个简单的常用的获取帮助的指令,git help,查看git的帮助信息,另一个更为重要的指令是git help <command>,查看指定的command的帮助信息。

    下面是git help的信息:

     1 [root@CloudGame mrepos]# git help
     2 usage: git [--version] [--help] [-C <path>] [-c name=value]
     3            [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
     4            [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
     5            [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
     6            <command> [<args>]
     7 
     8 These are common Git commands used in various situations:
     9 
    10 start a working area (see also: git help tutorial)
    11    clone      Clone a repository into a new directory
    12    init       Create an empty Git repository or reinitialize an existing one
    13 
    14 work on the current change (see also: git help everyday)
    15    add        Add file contents to the index
    16    mv         Move or rename a file, a directory, or a symlink
    17    reset      Reset current HEAD to the specified state
    18    rm         Remove files from the working tree and from the index
    19 
    20 examine the history and state (see also: git help revisions)
    21    bisect     Use binary search to find the commit that introduced a bug
    22    grep       Print lines matching a pattern
    23    log        Show commit logs
    24    show       Show various types of objects
    25    status     Show the working tree status
    26 
    27 grow, mark and tweak your common history
    28    branch     List, create, or delete branches
    29    checkout   Switch branches or restore working tree files
    30    commit     Record changes to the repository
    31    diff       Show changes between commits, commit and working tree, etc
    32    merge      Join two or more development histories together
    33    rebase     Forward-port local commits to the updated upstream head
    34    tag        Create, list, delete or verify a tag object signed with GPG
    35 
    36 collaborate (see also: git help workflows)
    37    fetch      Download objects and refs from another repository
    38    pull       Fetch from and integrate with another repository or a local branch
    39    push       Update remote refs along with associated objects
    View Code

    下面是git help clone的信息,你也可以查看其他的command,例如branch,add等等。。。

     1 GIT-CLONE(1)                      Git Manual                      GIT-CLONE(1)
     2 
     3 NAME
     4        git-clone - Clone a repository into a new directory
     5 
     6 SYNOPSIS
     7        git clone [--template=<template_directory>]
     8                  [-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror]
     9                  [-o <name>] [-b <name>] [-u <upload-pack>] [--reference <repository>]
    10                  [--depth <depth>] [--recursive] [--] <repository> [<directory>]
    11 
    12 DESCRIPTION
    13        Clones a repository into a newly created directory, creates remote-tracking branches for each branch in the cloned
    14        repository (visible using git branch -r), and creates and checks out an initial branch that is forked from the cloned
    15        repository’s currently active branch.
    16 
    17        After the clone, a plain git fetch without arguments will update all the remote-tracking branches, and a git pull
    18        without arguments will in addition merge the remote master branch into the current master branch, if any.
    19 
    20        This default configuration is achieved by creating references to the remote branch heads under refs/remotes/origin and
    21        by initializing remote.origin.url and remote.origin.fetch configuration variables.
    22 
    23 OPTIONS
    24        --local, -l
    25            When the repository to clone from is on a local machine, this flag bypasses the normal "git aware" transport
    26            mechanism and clones the repository by making a copy of HEAD and everything under objects and refs directories. The
    27            files under .git/objects/ directory are hardlinked to save space when possible. This is now the default when the
    28            source repository is specified with /path/to/repo syntax, so it essentially is a no-op option. To force copying
    29            instead of hardlinking (which may be desirable if you are trying to make a back-up of your repository), but still
    30            avoid the usual "git aware" transport mechanism, --no-hardlinks can be used.
    31 
    32        --no-hardlinks
    33            Optimize the cloning process from a repository on a local filesystem by copying files under .git/objects directory.
    34 
    35        --shared, -s
    36            When the repository to clone is on the local machine, instead of using hard links, automatically setup
    37            .git/objects/info/alternates to share the objects with the source repository. The resulting repository starts out
    38            without any object of its own.
    39 ..............................
    View Code

    3. 克隆远程指定版本的分支到本地(例如,从github上clone版本为2.1.X分支的源码到本地)git clone url [-b <branch name>]

    1 [root@CloudGame gatling217]# git clone https://github.com/gatling/gatling -b 2.1.X
    2 Cloning into 'gatling'...
    3 remote: Counting objects: 116304, done.
    4 remote: Compressing objects: 100% (1808/1808), done.
    5 remote: Total 116304 (delta 844), reused 5 (delta 5), pack-reused 113985
    6 Receiving objects: 100% (116304/116304), 27.23 MiB | 3.53 MiB/s, done.
    7 Resolving deltas: 100% (37493/37493), done.
    8 Checking connectivity... done.

    其中,git clone指令,也有很多其他的用法,较常用的就是克隆远程分支到本地指定的路径,git clone url [<local dir>],这里的local dir会自动创建出来,不用事先创建

    [root@CloudGame gatling217]# git clone https://github.com/gatling/gatling gatlingMaster
    Cloning into 'gatlingMaster'...
    remote: Counting objects: 116304, done.
    remote: Compressing objects: 100% (1808/1808), done.
    remote: Total 116304 (delta 844), reused 5 (delta 5), pack-reused 113985
    Receiving objects: 100% (116304/116304), 27.23 MiB | 2.78 MiB/s, done.
    Resolving deltas: 100% (37493/37493), done.
    Checking connectivity... done.

    4. 查看当前项目所在的分支(本地信息) git branch

    1 [root@CloudGame autoscale]# git branch
    2 * autoscale
    3   master

    5. 查看当前项目的本地/远程仓库所有的分支信息git branch -a

     1 [root@CloudGame ngrinder]# git branch -a
     2 * autoscale-preventfix
     3   remotes/origin/HEAD -> origin/master
     4   remotes/origin/add_sticky_copyright
     5   remotes/origin/agent_download
     6   remotes/origin/allow_user_registration
     7   remotes/origin/autoagent
     8   remotes/origin/autoscale
     9   remotes/origin/autoscale-preventfix
    10   remotes/origin/b38
    11   remotes/origin/bootstrap-3-trial
    12   remotes/origin/compact_page_with_macro
    13   remotes/origin/gh-pages
    14   remotes/origin/improve_naming
    15   remotes/origin/issue-58
    16   remotes/origin/jdk8-support
    17   remotes/origin/make_unit_test_work
    18   remotes/origin/master
    19   remotes/origin/monitor_plugin_extention
    20   remotes/origin/ngrinder_3.0
    21   remotes/origin/ngrinder_3.1
    22   remotes/origin/ngrinder_3.1_feature_demo
    23   remotes/origin/release_3.3
    24   remotes/origin/speedy_user_switch

    其中,git branch指令还有一些其他的option,都是很重要的,比较常用的,比如创建分支,删除分支,改名等等,如下面的帮助列表:

     1     -d
     2            Delete a branch. The branch must be fully merged in its upstream branch, or in HEAD if no upstream was set with
     3            --track or --set-upstream.
     4 
     5        -D
     6            Delete a branch irrespective of its merged status.
     7 
     8        -l
     9            Create the branch’s reflog. This activates recording of all changes made to the branch ref, enabling use of date
    10            based sha1 expressions such as "<branchname>@{yesterday}". Note that in non-bare repositories, reflogs are usually
    11            enabled by default by the core.logallrefupdates config option.
    12 
    13        -f, --force
    14            Reset <branchname> to <startpoint> if <branchname> exists already. Without -f git branch refuses to change an
    15            existing branch.
    16 
    17        -m
    18            Move/rename a branch and the corresponding reflog.
    19 
    20        -M
    21            Move/rename a branch even if the new branch name already exists.
    22 
    23        --color[=<when>]
    24            Color branches to highlight current, local, and remote branches. The value must be always (the default), never, or
    25            auto.
    26 
    27        --no-color
    28            Turn off branch colors, even when the configuration file gives the default to color output. Same as --color=never.
    29 
    30        -r
    31            List or delete (if used with -d) the remote-tracking branches.
    32 
    33        -a
    34            List both remote-tracking branches and local branches.
    35 
    36        -v, --verbose
    37            Show sha1 and commit subject line for each head, along with relationship to upstream branch (if any). If given
    38            twice, print the name of the upstream branch, as well.

    6. 创建一个远程仓库,并做一些常见的操作。

    [root@CloudGame tmp]# mkdir mrepos      //A. 创建本地仓库目录
    [root@CloudGame tmp]# cd mrepos/
    [root@CloudGame mrepos]# ll
    total 0
    [root@CloudGame mrepos]# git init      //B。 初始化本地仓库
    Initialized empty Git repository in /mnt/tmp/mrepos/.git/
    [root@CloudGame mrepos]# ll
    total 0
    [root@CloudGame mrepos]# ll -al
    total 12
    drwxr-xr-x 3 root root 4096 Jan 11 08:58 .
    drwxr-xr-x 4 root root 4096 Jan 11 08:58 ..
    drwxr-xr-x 7 root root 4096 Jan 11 08:58 .git
    [root@CloudGame mrepos]# vim README.txt  //C。 在本地编辑一个文件,用于后面的操作
    [root@CloudGame mrepos]# 
    [root@CloudGame mrepos]# 
    [root@CloudGame mrepos]# git status    //D。查看当前分支的状态信息
    On branch master
    
    Initial commit
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        README.txt
    
    nothing added to commit but untracked files present (use "git add" to track)

    接下来,将文件添加到本地仓库并commit,为后续提交做准备。

     1 [root@CloudGame mrepos]# git add README.txt     //E。 将文件添加到index,就是为commit准备
     2 [root@CloudGame mrepos]# git status          //F。 再次查看当前分支的状态
     3 On branch master
     4 
     5 Initial commit
     6 
     7 Changes to be committed:
     8   (use "git rm --cached <file>..." to unstage)
     9 
    10     new file:   README.txt
    11 
    12 [root@CloudGame mrepos]# git commit -m "First commit in demo about create remote repos"    //G。 将当前的改变记录到仓库
    13 [master (root-commit) 231d9dc] First commit in demo about create remote repos
    14  1 file changed, 1 insertion(+)
    15  create mode 100644 README.txt

    现在,将本地仓库添加为远程仓库,这个,我觉得在很多单位或者团队开发过程中都会用到,实现版本集中管理,多地开发。

     1 [root@CloudGame mrepos]# git remote add origin ssh://root@109.105.5.108/mnt/tmp/mrepos    //H。 添加本地仓库到远程仓库
     2 [root@CloudGame mrepos]# git push origin master                           //I。更新远程仓库origin的refspec为master的refs
     3 root@109.105.5.108's password: 
     4 Everything up-to-date
     5 [root@CloudGame mrepos]# git remote show origin                  //J. 查看远程仓库origin的信息
     6 root@109.105.5.108's password: 
     7 * remote origin
     8   Fetch URL: ssh://root@109.105.5.108/mnt/tmp/mrepos
     9   Push  URL: ssh://root@109.105.5.108/mnt/tmp/mrepos
    10   HEAD branch: master
    11   Remote branch:
    12     master tracked
    13   Local ref configured for 'git push':
    14     master pushes to master (up to date)

    这里,有个很重要的地方就是git remote add命令参数,本例子中,origin是仓库的名字,你也可以取你自己的名字,但是通常将远程仓库的名字取名为origin。再就是后面跟着的URL,这个是要供给其他人访问的。所以,地址一定要是个绝对地址,尤其是后面的路径。

    觉得origin仓库名不好或者不需要了,可以执行git remote rm origin进行删除。这里是删除远程的仓库哟。本地仓库删除,可以直接删除目录即可。

    这里,要注意的还有,上述的URL中,其格式可以是很多种(),如下:

     1 GIT URLS
     2        In general, URLs contain information about the transport protocol, the address of the remote server, and the path to
     3        the repository. Depending on the transport protocol, some of this information may be absent.
     4 
     5        Git natively supports ssh, git, http, https, ftp, ftps, and rsync protocols. The following syntaxes may be used with
     6        them:
     7 
     8        ·   ssh://[user@]host.xz[:port]/path/to/repo.git/
     9 
    10        ·   git://host.xz[:port]/path/to/repo.git/
    11 
    12        ·   http[s]://host.xz[:port]/path/to/repo.git/
    13 
    14        ·   ftp[s]://host.xz[:port]/path/to/repo.git/
    15 
    16        ·   rsync://host.xz/path/to/repo.git/
    17 
    18        An alternative scp-like syntax may also be used with the ssh protocol:
    19 
    20        ·   [user@]host.xz:path/to/repo.git/
    21 
    22        The ssh and git protocols additionally support ~username expansion:
    23 
    24        ·   ssh://[user@]host.xz[:port]/~[user]/path/to/repo.git/
    25 
    26        ·   git://host.xz[:port]/~[user]/path/to/repo.git/
    27 
    28        ·   [user@]host.xz:/~[user]/path/to/repo.git/
    29 
    30        For local respositories, also supported by git natively, the following syntaxes may be used:
    31 
    32        ·   /path/to/repo.git/
    33 
    34        ·    file:///path/to/repo.git/

    现在来测试一下,这个库创建的是否可用(,可以在自己本地的另外一个终端测试,也可以在另外一台机器上测试,我都测试过,都没有问题):

    1 [root@CloudGame mytool]# git clone ssh://root@109.105.5.108/mnt/tmp/mrepos
    2 Cloning into 'mrepos'...
    3 root@109.105.5.108's password: 
    4 remote: Counting objects: 3, done.
    5 remote: Total 3 (delta 0), reused 0 (delta 0)
    6 Receiving objects: 100% (3/3), done.
    7 Checking connectivity... done.

    7. 创建并提交分支

    1 [root@CloudGame mrepos]# git checkout -b firstbranch
    2 Switched to a new branch 'firstbranch'
    3 [root@CloudGame mrepos]# git branch
    4 * firstbranch
    5   master

    现在创建一个文件并做提交到远程仓库:

     1 [root@CloudGame mrepos]# vim firstfile.txt
     2 [root@CloudGame mrepos]# git add .
     3 [root@CloudGame mrepos]# git commit -m "first file"
     4 [firstbranch a283ff2] first file
     5  1 file changed, 1 insertion(+)
     6  create mode 100644 firstfile.txt
     7 [root@CloudGame mrepos]# git push
     8 fatal: The current branch firstbranch has no upstream branch.
     9 To push the current branch and set the remote as upstream, use
    10 
    11     git push --set-upstream origin firstbranch
    12 
    13 [root@CloudGame mrepos]# git push--set-upstream origin firstbranch
    14 git: 'push--set-upstream' is not a git command. See 'git --help'.
    15 [root@CloudGame mrepos]# git push --set-upstream origin firstbranch
    16 root@109.105.5.108's password: 
    17 Branch firstbranch set up to track remote branch firstbranch from origin.
    18 Everything up-to-date
    19 [root@CloudGame mrepos]# git status
    20 On branch firstbranch
    21 Your branch is up-to-date with 'origin/firstbranch'.
    22 nothing to commit, working directory clean

    通过图形化界面可以看看,创建的分支基本信息:

    现在再在另外一个窗口或者另外一个机器上进行clone刚提交的内容,并做分支切换,检测是否正确:

    [root@CloudGame mrepos]# ll
    total 8
    -rw-r--r-- 1 root root 20 Jan 11 10:28 firstfile.txt
    -rw-r--r-- 1 root root 12 Jan 11 10:28 README.txt
    [root@CloudGame mrepos]# git branch      //查看当前所在的分支
    * firstbranch
    [root@CloudGame mrepos]# git checkout master  //切换到master分支上
    Branch master set up to track remote branch master from origin.
    Switched to a new branch 'master'
    [root@CloudGame mrepos]# ll
    total 4
    -rw-r--r-- 1 root root 12 Jan 11 10:28 README.txt
    [root@CloudGame mrepos]# 

    最后,再介绍一个从指定分支的指定commit点拉分支的操作。这个还是非常有用的。首先在本地的firstbranch分支下创建一个文件secondfile.txt,并push到远程仓库。这个和创建firstfile.txt一样。不列出操作,主要看看在另一个窗口/另外一个机器上pull这个文件的操作,因为前面没有提到如何从远程仓库的某个分支上取文件。看下面的信息:

     1 [root@CloudGame mrepos]# git checkout firstbranch    //切换到firstbranch
     2 Switched to branch 'firstbranch'
     3 Your branch is up-to-date with 'origin/firstbranch'.
     4 [root@CloudGame mrepos]# ll                 //目前没有看到刚才提交的secondfile.txt
     5 total 8
     6 -rw-r--r-- 1 root root 20 Jan 11 10:41 firstfile.txt
     7 -rw-r--r-- 1 root root 12 Jan 11 10:28 README.txt
     8 [root@CloudGame mrepos]# git pull              //pull一下,就可以将远程仓库的内容更新下来
     9 root@109.105.5.108's password: 
    10 remote: Counting objects: 3, done.
    11 remote: Compressing objects: 100% (2/2), done.
    12 remote: Total 3 (delta 0), reused 0 (delta 0)
    13 Unpacking objects: 100% (3/3), done.
    14 From ssh://109.105.5.108/mnt/tmp/mrepos
    15    a283ff2..fc6f513  firstbranch -> origin/firstbranch
    16 Updating a283ff2..fc6f513
    17 Fast-forward
    18  secondfile.txt | 1 +
    19  1 file changed, 1 insertion(+)
    20  create mode 100644 secondfile.txt
    21 [root@CloudGame mrepos]# ll                 //现在是不是看到了secondfile.txt了
    22 total 12
    23 -rw-r--r-- 1 root root 20 Jan 11 10:41 firstfile.txt
    24 -rw-r--r-- 1 root root 12 Jan 11 10:28 README.txt
    25 -rw-r--r-- 1 root root 21 Jan 11 10:41 secondfile.txt

    在此,我就基于firstfile.txt文件提交的commit点,在另外一个终端创建一个新的分支secondbranch。注意,这里的commit点如何获取呢,请看上面的图形化界面截图中的SHA1 ID后面的字符串,那个就是你要的startpoint的标记,或者说是svn/clearcase等里面的label。

    没有创建分支时,看看远程版本树上的分支信息:

    创建此类分支的指令和操作过程如下:

     1 [root@CloudGame mrepos]# git branch
     2 * firstbranch
     3   master
     4 [root@CloudGame mrepos]# git checkout -b secondbranch a283ff298dbff855b7e2cca60f8a5c25d0cca5f2    
     5 Switched to a new branch 'secondbranch'
     6 [root@CloudGame mrepos]# git branch
     7   firstbranch
     8   master
     9 * secondbranch
    10 [root@CloudGame mrepos]# ll
    11 total 8
    12 -rw-r--r-- 1 root root 20 Jan 11 10:41 firstfile.txt
    13 -rw-r--r-- 1 root root 12 Jan 11 10:28 README.txt

    是不是发现,secondfile.txt不见了,对,这个就是版本管理中的视图管理,因为当前的分支节点上一了一个commit。然后创建一个文件并提交到远程仓库:

     1 [root@CloudGame mrepos]# git push --set-upstream origin secondbranch
     2 root@109.105.5.108's password: 
     3 Counting objects: 3, done.
     4 Delta compression using up to 4 threads.
     5 Compressing objects: 100% (2/2), done.
     6 Writing objects: 100% (3/3), 330 bytes | 0 bytes/s, done.
     7 Total 3 (delta 0), reused 0 (delta 0)
     8 To ssh://root@109.105.5.108/mnt/tmp/mrepos
     9  * [new branch]      secondbranch -> secondbranch
    10 Branch secondbranch set up to track remote branch secondbranch from origin.

    再通过gitk命令看看图形化界面的内容,你会发现多了一个分支secondbranch。

    8. 如何删除一个分支呢

     下面还是通过操作说明,就将前面创建的firstbranch删除吧,删除本地,还要删除远程的。

     1 [root@CloudGame mrepos]# git branch -a      //注意,git是不让你删除当前所在的branch的
     2 * firstbranch
     3   master
     4   remotes/origin/HEAD -> origin/firstbranch
     5   remotes/origin/firstbranch
     6   remotes/origin/master
     7 [root@CloudGame mrepos]# git checkout master
     8 Switched to branch 'master'
     9 Your branch is up-to-date with 'origin/master'.
    10 [root@CloudGame mrepos]# 
    11 [root@CloudGame mrepos]# 
    12 [root@CloudGame mrepos]# git branch -D firstbranch    //删除本地的分支
    13 Deleted branch firstbranch (was 26e41de).
    14 [root@CloudGame mrepos]# git branch -r -d origin/firstbranch    //删除远程仓库中的分支
    15 Deleted remote-tracking branch origin/firstbranch (was 26e41de).
    16 [root@CloudGame mrepos]# git push origin :firstbranch        //将删除的内容提交到远程仓库,注意,冒号前面的空格不能忘记了哟,否则会出错的
    17 root@109.105.5.108's password: 
    18 remote: error: refusing to update checked out branch: refs/heads/firstbranch
    19 remote: error: By default, updating the current branch in a non-bare repository
    20 remote: error: is denied, because it will make the index and work tree inconsistent
    21 remote: error: with what you pushed, and will require 'git reset --hard' to match
    22 remote: error: the work tree to HEAD.
    23 remote: error: 
    24 remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
    25 remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
    26 remote: error: its current branch; however, this is not recommended unless you
    27 remote: error: arranged to update its work tree to match what you pushed in some
    28 remote: error: other way.
    29 remote: error: 
    30 remote: error: To squelch this message and still keep the default behaviour, set
    31 remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
    32 To ssh://root@109.105.5.108/mnt/tmp/mrepos
    33  ! [remote rejected] firstbranch (branch is currently checked out)
    34 error: failed to push some refs to 'ssh://root@109.105.5.108/mnt/tmp/mrepos'
    35 [root@CloudGame mrepos]# 
    36 [root@CloudGame mrepos]# git push origin :firstbranch  //上面的错误信息是说,要删除的分支目前出于checkout的状态,不许删除,只能删除远程中非checkout状态的分支
    37 root@109.105.5.108's password: 
    38 To ssh://root@109.105.5.108/mnt/tmp/mrepos
    39  - [deleted]         firstbranch

    到此,是不是基本的常用的日常工作中用到的git操作,在这里都涉及到了,希望对博友有所帮助,对我也是一个备忘记录吧。

  • 相关阅读:
    L110 promise
    2018.7.7 MBA -从专业到管理(1)—— 技术人才与的管理人才比较
    2018.7.9 AVR-BAT program
    2018.7.6 TX射频调试-PP
    红外伪装(利用石墨烯通电)
    2018.7.2 AK22 不良品分析
    2018.6.25会议跟进
    2018.6.24异常跟进
    PyQt(Python+Qt)学习随笔:Model/View中的枚举类 Qt.MatchFlag的取值及含义
    PyQt(Python+Qt)学习随笔:基于项的项部件(Item Widgets(Item-Based))概述
  • 原文地址:https://www.cnblogs.com/shihuc/p/5120516.html
Copyright © 2011-2022 走看看