git简介及上传代码到github

Posted 张行之

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了git简介及上传代码到github相关的知识,希望对你有一定的参考价值。

git是常用的版本控制工具,对于git你需要了解它的几个重要的概念——工作区,版本库,暂存区,master

  • 工作区
    -工作区指的是你在电脑里能看到的目录(.git文件夹是git的版本库,是git初始化时生成的),如下图:
    技术分享
  • 版本库
  • 工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库。
    技术分享

  • 暂存区
    就是称为stage(或者叫index)的。

  • master
    master是Git为我们自动创建的第一个分支,以及指向master的一个指针叫HEAD。

我们通常在工作区进行增删改内容后,需要通过git add命令,把文件添加到暂存区中,通过git commit命令把文件提交到master中。这样git就会保存你的记录。

git常用的几个命令

  • git init
    -初始化git,在文件夹中生成.git的隐藏文件夹

  • git add 文件名/option(参数)

    • 这些都是add 命令的参数,我一般用-A,
    --n, --dry-run         dry run
    -v, --verbose          be verbose
    -i, --interactive     interactive picking
    -p, --patch           select hunks interactively
    -e, --edit            edit current diff and apply
    -f, --force           allow adding otherwise ignored files
    -u, --update          update tracked files
    -N, --intent-to-add   record only the fact that the path will be added later
    -A, --all             add changes from all tracked and untracked files
    --ignore-removal      ignore paths removed in the working tree (same as --no-all)
    --refresh             don‘t add, only refresh the index
    --ignore-errors       just skip files which cannot be added because of errors
    --ignore-missing      check if - even missing - files are ignored in dry run
  • git commit option(参数,根据实际情况添加) -m”提交的描述信息”或者-m’#’
    • ’#’表示没有提交信息,或忽略提交信息。
      -m不写是不会让你提交的,提交会失败。
      option参数有:
-q, --quiet           suppress summary after successful commit
-v, --verbose         show diff in commit message template

Commit message options
    -F, --file <file>     read message from file
    --author <author>     override author for commit
    --date <date>         override date for commit
    -m, --message <message>
                          commit message
    -c, --reedit-message <commit>
                          reuse and edit message from specified commit
    -C, --reuse-message <commit>
                          reuse message from specified commit
    --fixup <commit>      use autosquash formatted message to fixup specified commit
    --squash <commit>     use autosquash formatted message to squash specified commit
    --reset-author        the commit is authored by me now (used with -C/-c/--amend)
    -s, --signoff         add Signed-off-by:
    -t, --template <file>
                          use specified template file
    -e, --edit            force edit of commit
    --cleanup <default>   how to strip spaces and #comments from message
    --status              include status in commit message template
    -S, --gpg-sign[=<key-id>]
                          GPG sign commit
Commit contents options
    -a, --all             commit all changed files
    -i, --include         add specified files to index for commit
    --interactive         interactively add files
    -p, --patch           interactively add changes
    -o, --only            commit only specified files
    -n, --no-verify       bypass pre-commit hook
    --dry-run             show what would be committed
    --short               show status concisely
    --branch              show branch information
    --porcelain           machine-readable output
    --long                show status in long format (default)
    -z, --null            terminate entries with NUL
    --amend               amend previous commit
    --no-post-rewrite     bypass post-rewrite hook
    -u, --untracked-files[=<mode>]
                          show untracked files, optional modes: all, normal, no. (Default: all)
  • git status
  • 用来查看git的状态,一般commit后会显示
On branch master
nothing to commit, working directory clean

如果是add后,会显示

warning: CRLF will be replaced by LF in 1.txt.
The file will have its original line endings in your working directory.
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   1.txt(这是我测试的文件)
  • git log
    • 用来查看提交的信息
$ git log
commit d82fce6bc6a140ca09390918de6afb40642f1133
Author: xxx(这里显示的是你git账号信息)
Date:   Sun Mar 27 11:00:13 2016 +0800

    #(提交信息)

commit 7d525203a7e81171596e3c5802150a5190121566
Author: xxx
Date:   Sun Mar 27 10:41:05 2016 +0800

    111(提交信息)
  • git reflog
    • 也是用来查看提交信息的
$ git reflog
d82fce6 HEAD@{0}: commit: #
7d52520 HEAD@{1}: commit (initial): 111

d82fce6 –类是于每次提交的id,可以通过id恢复到对应的版本,也就是
commit d82fce6bc6a140ca09390918de6afb40642f1133的前7位。

  • git reset
    • 返回以前版本–指定一个版本(通过id)
    • git reset –hard d82fce6(返回指定版本)
    • $ git reset –hard HEAD^(返回上一个版本,^有几个就表示返回上几个版本)
$ git reset --hard d82fce6
HEAD is now at d82fce6 #
$ git reset --hard HEAD^
HEAD is now at 7d52520 111
  • git –help
    • 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.
  • cat 文件名(包括拓展名)
    查看单个文件的内容

git生成ssh证书

  • $ ssh-keygen -t rsa -C “[email protected]
    • 你需要把邮件地址换成你自己的邮件地址,然后一路回车
      你需要把邮件地址换成你自己的邮件地址,然后一路回车,使用默认值即可,由于这个Key也不是用于军事目的,所以也无需设置密码。

如果一切顺利的话,可以在用户主目录里找到.ssh目录,里面有id_rsa和id_rsa.pub两个文件,这两个就是SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人。
技术分享

技术分享


使用github托管代码

  • github将在git上生成的ssh的公约内容,在github上认证

    • 如图
      技术分享

      技术分享

技术分享

  • github创建repository
    技术分享
    技术分享

  • git remote add origin [email protected]:github用户名/xxx.git(非常重要)
    这是将git代码托管到github.com网站上的命令,指定托管的位置。
    或者使用
    git remote add origin https://github.com/github用户名/xxx.git
    二者的协议不同,前者是ssh协议,或者是https协议。

  • git push -u origin master

    • 将文件上传到github.com
      ——不过在push的时候会报错,我也没解决,多试几次就好。——

文章参考:
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000




























以上是关于git简介及上传代码到github的主要内容,如果未能解决你的问题,请参考以下文章

Github简介

git基本操作及上传代码到gitHub

学习Git的一点心得以及如何把本地修改删除的代码上传到github中

初识 GitHub · 简介篇

Github介绍与使用

Github介绍与使用