您的位置:首页 > 其它

新手初次使用Git实战经历

2016-01-14 10:53 302 查看
下载了Git-2.6.4-32-bit,大概29M左右,第一次使用git开始~~~

首先建议先看一下 git简明指南http://rogerdudler.github.io/git-guide/index.zh.html) ,弄清git的工作流!要不然命令看起来可能会犯糊涂。

第一个命令我敲了git help,主要看看它有哪些命令

[code]Administrator@PDPQV5D3CUSX7ZL MINGW32 ~
$ git help
usage: git [--version] [--help] [-C <path>] [-c name=value]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone      Clone a repository into a new directory
   init       Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add        Add file contents to the index
   mv         Move or rename a file, a directory, or a symlink
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect     Use binary search to find the commit that introduced a bug
   grep       Print lines matching a pattern
   log        Show commit logs
   show       Show various types of objects
   status     Show the working tree status

grow, mark and tweak your common history
   branch     List, create, or delete branches
   checkout   Switch branches or restore working tree files
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   merge      Join two or more development histories together
   rebase     Forward-port local commits to the updated upstream head
   tag        Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch      Download objects and refs from another repository
   pull       Fetch from and integrate with another repository or a local branch
   push       Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.


由上,我试着敲出了 git –version命令

[code]Administrator@PDPQV5D3CUSX7ZL MINGW32 ~
$ git --version
git version 2.6.4.windows.1


接着,按照网上步骤,填写用户名和邮箱

[code]Administrator@PDPQV5D3CUSX7ZL MINGW32 ~
$ git config --glabal user.name "arviklinux"
error: unknown option `glabal'
usage: git config [<options>]

Config file location
    --global              use global config file
    --system              use system config file
    --local               use repository config file
    -f, --file <file>     use given config file
    --blob <blob-id>      read config from given blob object

Action
    --get                 get value: name [value-regex]
    --get-all             get all values: key [value-regex]
    --get-regexp          get values for regexp: name-regex [value-regex]
    --get-urlmatch        get value specific for the URL: section[.var] URL
    --replace-all         replace all matching variables: name value [value_regex]
    --add                 add a new variable: name value
    --unset               remove a variable: name [value-regex]
    --unset-all           remove all matches: name [value-regex]
    --rename-section      rename section: old-name new-name
    --remove-section      remove a section: name
    -l, --list            list all
    -e, --edit            open an editor
    --get-color           find the color configured: slot [default]
    --get-colorbool       find the color setting: slot [stdout-is-tty]

Type
    --bool                value is "true" or "false"
    --int                 value is decimal number
    --bool-or-int         value is --bool or --int
    --path                value is a path (file or directory name)

Other
    -z, --null            terminate values with NUL byte
    --name-only           show variable names only
    --includes            respect include directives on lookup


glabal打错,git给出了使用方法,于是再来一遍

[code]Administrator@PDPQV5D3CUSX7ZL MINGW32 ~
$ git config --global user.name "arviklinux"

Administrator@PDPQV5D3CUSX7ZL MINGW32 ~
$ git config --global user.email "1216601195@qq.com"


于是,开始创建版本库,我的是在E盘下创建了github文件夹

[code]Administrator@PDPQV5D3CUSX7ZL MINGW32 ~
$ cd E:

Administrator@PDPQV5D3CUSX7ZL MINGW32 /e
$ cd github

Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github (master)
$ pwd
/e/github


此时已经出现了master(主干)。我在github文件夹下新建目录test,进入test,再新建readme.txt。

[code]Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github (master)
$ mkdir test

Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github (master)
$ cd ./test/
touch readme.tex
Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github/test (master)
$ touch readme.txt

Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github/test (master)
$ ls
readme.txt

Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github/test (master)
$ git add readme.txt


此时查看一下状态,用git status命令

[code]Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github/test (master)
$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   readme.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        ../router/


由于之前在github下建立了router目录,所以显示router,可忽略。

此时提交readme.txt到暂存区(本地),命令如下

[code]Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github/test (master)
$ git commit -m "readme.txt commit by arvik"
[master (root-commit) 819faf8] readme.txt commit by arvik
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test/readme.txt


接着再次查看状态

[code]Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github/test (master)
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        ../router/

nothing added to commit but untracked files present (use "git add" to track)


更改一下readme.txt文件,

[code]Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github/test (master)
$ echo "the first change by arvik" > readme.txt

Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github/test (master)
$ cat ./readme.txt
the first change by arvik


再次,git status

[code]Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github/test (master)
$ git status
On branch master
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:   readme.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        ../router/

no changes added to commit (use "git add" and/or "git commit -a")


可以看出,git已经发现readme.txt被改动过了。使用git diff查看前后改动了什么

[code]Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github/test (master)
$ git diff readme.txt
diff --git a/test/readme.txt b/test/readme.txt
index e69de29..4f7bf02 100644
--- a/test/readme.txt
+++ b/test/readme.txt
@@ -0,0 +1 @@
+the first change by arvik
warning: LF will be replaced by CRLF in test/readme.txt.
The file will have its original line endings in your working directory.


提交到head(本地),使用git add命令

[code]Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github/test (master)
$ git add ./readme.txt & git commit -m "readme.txt first change"
[1] 3588
warning: LF will be replaced by CRLF in test/readme.txt.
The file will have its original line endings in your working directory.
[master warning: LF will be replaced by CRLF in test/readme.txt.
The file will have its original line endings in your working directory.
1cc7fb7] readme.txt first change
warning: LF will be replaced by CRLF in test/readme.txt.
The file will have its original line endings in your working directory.
 1 file changed, 1 insertion(+)
[1]+  Done                    git add ./readme.txt


使用git log查看历史记录

[code]$ git log
commit 1cc7fb7ef8eb2d51918a0d40bd03f81e963f228f
Author: arviklinux <1216601195@qq.com>
Date:   Tue Dec 29 10:32:31 2015 +0800

    readme.txt first change

commit 819faf8affb11309f826464c90f5c662a6245b81
Author: arviklinux <1216601195@qq.com>
Date:   Tue Dec 29 10:27:12 2015 +0800

    readme.txt commit by arvik


准备连接远程仓库,已经在github注册了账号arviklinux,github网站上非常清楚(https://help.github.com/articles/generating-ssh-keys/)。按照以下步骤,生成一个新的ssh秘钥

[code]Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github/test (master)
$ ssh-keygen -t rsa -b 4096 -C "1216601195@qq.com"


这里贴个图,



添加秘钥到ssh

[code]Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github/test (master)
$ ssh-agent -s
SSH_AUTH_SOCK=/tmp/ssh-AbsYIfsyNZUp/agent.3632; export SSH_AUTH_SOCK;
SSH_AGENT_PID=6040; export SSH_AGENT_PID;
echo Agent pid 6040;

Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github/test (master)
$ ssh-add ~/ssh/id_rsa
Could not open a connection to your authentication agent.


提示我连接失败,原因是忘了中间一步

[code]Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github (master)
$ eval `ssh-agent`
Agent pid 9436

Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github (master)
$ ssh-add ~/.ssh/id_rsa
Enter passphrase for /c/Users/Administrator/.ssh/id_rsa:


发现还不对,到自己的github主页将ssh秘钥添加上去,然后再ssh-add

[code]Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github (master)
$ ssh-add ~/.ssh/id_rsa
Enter passphrase for /c/Users/Administrator/.ssh/id_rsa:
Bad passphrase, try again for /c/Users/Administrator/.ssh/id_rsa:
Bad passphrase, try again for /c/Users/Administrator/.ssh/id_rsa:
Bad passphrase, try again for /c/Users/Administrator/.ssh/id_rsa:
Identity added: /c/Users/Administrator/.ssh/id_rsa (/c/Users/Administrator/.ssh/id_rsa)


现在才发现原因是密码填错了N次,,,,

OK了,先前一步在github主页将ssh秘钥添加上去,此步就不必了。

复制一个远程仓库到本地

[code]Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github (master)
$ git clone https://github.com/arviklinux/Intelligent-router Cloning into 'Intelligent-router'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 3
Unpacking objects: 100% (3/3), done.
Checking connectivity... done.


成功!

进入该目录,并且git push

[code]Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github (master)
$ cd ./Intelligent-router/

Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github/Intelligent-router (master)
$ git push -u origin master
Branch master set up to track remote branch master from origin.
Everything up-to-date


在本地github/Intelligent-router/目录新建了一个文件夹ZNdomain,并拷贝了一些文件进去,提交

[code]Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github/Intelligent-router (master)
$ git add ZNdomain_login/

Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github/Intelligent-router (master)
$ git commit -m "submit Interlligent-router files"
[master 239ce14] submit Interlligent-router files
 7 files changed, 556 insertions(+)
 create mode 100644 ZNdomain_login/Makefile
 create mode 100644 ZNdomain_login/domain_hijack.c
 create mode 100644 ZNdomain_login/domain_hijack.h
 create mode 100644 ZNdomain_login/domain_map.c
 create mode 100644 ZNdomain_login/domain_map.h
 create mode 100644 ZNdomain_login/forgeDNS.c
 create mode 100644 ZNdomain_login/forgeDNS.h


最后,推送到远程仓库

[code]Administrator@PDPQV5D3CUSX7ZL MINGW32 /e/github/Intelligent-router (master)
$ git push origin master
Counting objects: 10, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (10/10), 4.51 KiB | 0 bytes/s, done.
Total 10 (delta 3), reused 0 (delta 0)
To https://github.com/arviklinux/Intelligent-router    61b4e81..239ce14  master -> master


至此,本篇实战经历结束!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: