zoukankan      html  css  js  c++  java
  • git入门基础

    git基础

    参考:
    官网git基础

    git 文件的生命周期

    文件的生命周期图:

    git中的文件可以分为4个阶段.

    • Untracked : 这是目录中没有被跟踪的文件,即不在git项目中,使用 git commit 等命令对文件进行提交时不会提交这样的文件。只有使用 git add file_name 将文件添加到git 工程中,该文件才会得到分享,转换到Staged状态。除了这个状态,其他3个状态指的都是被跟踪的文件

    • Unmodified:git工程中没有经过改动的文件,即up-to-date的文件,处于当前状态的文件一旦被改动,则将会变文 Modified 状态。使用 git remove 指令可以将该状态下的文件变成Untraked 状态。

    • Modified:被改动过的文件待添加到 Staged 中的文件。

    • Staged : 等待更新到下一个生命周期的文件,commit 命令将会将处于该状态的文件进行提交,确认更改,这样一来文件又重新回到未修改状态,Unmodified。

    git环境配置:

    $ git config --global user.name "John Doe"
    $ git config --global user.email johndoe@example.com
    $ git config --global core.editor emacs
    $ git config --list
    

    git 的基本命令

    转到需要初始化的目录下,运行下面的命令,初始化一个已经存在的目录为git目录.

    $ git init  
    

    从github上克隆一个工程目录,下面两条指令中的一个,区别在于是否另起目录名

    $ git clone /https://github.com/libgit2/libgit2  
    $ git clone /https://github.com/libgit2/libgit2 NewDirectoryName  
    

    获取文件状态 ,第二条指令表示状态的缩写版

    $ git status  
    $ git status -s 
    

    跟踪新的文件,文件状态从 untracked 变成 staged。

    $ git add YourNewFileName  
    

    在目录下添加.gitignore文件,可以设置自动忽略的文件GitHub的ignore文件示例

    查看已经被修改但没有Staged的文件

    $ git diff  
    

    查看staged中即将被commit的文件有什么不同

    $ git diff --staged  
    

    查看你目前已经Staged的所有文件

    $ git diff --cached  
    

    提交修改,即提交Staged状态下的文件
    _其第一条指令会在terminal打开默认的编辑器,让你写入相关信心,可以使用:git config --global core.editor 命令设置编辑器

    $ git commit   
    $ git commit -m 'some message about your committing'  
    

    删除文件

    >从目录删除  
    $ rm PROJECT.md             
    >从stage删除  
    $ git rm PROJECT.md   
    >不再跟踪文件       
    $ git rm --cached README     
    

    Moving Files

    $ git mv fileFrom fileTo  
    

    查看日志 $ git log



    撤销指令

    撤销上一次commit操作  
    $ git commit --amend  
    将sraged中的文件撤回(unstaging a staged file)  
    $ git reset HEAD CONTRIBUTING.md  
    撤销你所做的修改(discard the changes you've made)  
    $ git checkout -- CONTRIBUTING.md  
    

    远程服务器

    克隆一个远程项目    
    $ git clone https://github.com/schacon/ticgit  
    显示你的远程项目(Showing Your Remotes)  
    $ git remote -v  
    添加远程工程(Adding Remote Repositories)  
    命令句式:git remote add <shortname> <url>  
    $ git remote add pb https://github.com/paulboone/ticgit  
    Fetching and Pulling Your Remotes  
    $ git fetch [remote-name]  
    $ git push [remote-name] [branch-name]  
    Inspecting a Remote  
    $ git remote show [remote-name]
    $ git remote show origin  
    Removing and Renaming Remotes  
    $ git remote rename pb paul  
    Removing (git remote remove or git remote rm)
    $ git remote remove paul  
    

    Tagging

    list your Tags  
    $ git tag
    	v0.1
    	v1.3  
    search by pattern  
    $ git tag -l "v1.8.5*"  
    Create Tags and Annotated Tags  
    $ git tag -a v1.4 -m "my version 1.4" ß
    $ git show v1.4  
    

    Lighweight Tags,轻量标签只存储校验和

    $ git tag v1.4-lw  
    $ git tag
    

    为以前提交的版本设置标签,Tagging Later
    首先使用git log 查看每个版本的校验和,然后使用git tag根据校验和设置标签,例子如下所示:

    $ git log --pretty=oneline  
    15027957951b64cf874c3557a0f3547bd83b3ff6 Merge branch 'experiment'
    a6b4c97498bd301d84096da251c98a07c7723e65 beginning write support
    0d52aaab4479697da7686c15f77a3d64d9165190 one more thing
    6d52a271eda8725415634dd79daabbc4d9b6008e Merge branch 'experiment'
    0b7434d86859cc7b8c3d5e1dddfed66ff742fcbc added a commit function
    4682c3261057305bdd616e23b64b0857d832627b added a todo file
    166ae0c4d3f420721acbb115cc33848dfcc2121a started write support
    9fceb02d0ae598e95dc970b74767f19372d61af8 updated rakefile
    964f16d36dfccde844893cac5b347e7b3d44abbc commit the todo
    8a5cbc430f1a9c3d00faaeffd07798508422908a updated readme  
    $ git tag -a v1.2 9fceb02  
    

    分享标签,默认情况下,git push指令不会将标签上传到远程服务器上。这个操作需要自己显式的完成:git push origin [tagname]. 第二条指令式push所有标签。

    $ git push origin v1.5  
    $ git push origin --tags  
    

    移除标签,在git中不能真正的删除标签。只能添加一个新的分支。(git checkout -b [branchname] [tagname]: )

    $ git checkout -b version2 v2.0.0  
    Switched to a new branch 'version2'  
    

    Git Aliases

    git中可以为变量名或者项目取缩写名,这个可以参考官网。

    Github

    https://help.github.com/articles/adding-a-remote/

  • 相关阅读:
    冒泡型事件
    非IE浏览器中table标签出现边框的问题
    (转).NET使用一般处理程序生成验证码
    如何使用FreeTextBox
    如何使用DropDownList进行数据绑定并获取值
    (转).NET中获取字符串的MD5码
    (转).NET在后置代码中输入JS提示语句(背景不会变白)
    五大组件之activity(1)
    五大组件之activity(2)Activity的生命周期
    listview里子项有按钮的情况
  • 原文地址:https://www.cnblogs.com/fanling999/p/6692948.html
Copyright © 2011-2022 走看看