Git's Notes And Records

Author: Mikoy Date: 2018-02-04


1. The concepts of git:

  • Git: Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals.

  • Git Space:

    • <font color="blue">Working Directory:</font> The working directory is the place where files are checked out.
    • <font color=blue>Staging Area:</font> The staging area is a file, generally contained in your Git directory, that stores information about what will go into your next commit. Its technical name in Git parlance is the “index”, but the phrase “staging area” works just as well.
    • <font color=blue>Git Directory:</font> The Git directory is where Git stores the metadata(元数据) and object database for your project. This is the most important part of Git, and it is what is copied when you clone a repository from another computer.
      WorkFlow.png
  • Files Status:

    • Committed means that the data is safely stored in your local database.
    • Modified means that you have changed the file but have not committed it to your database yet.
    • Staged means that you have marked a modified file in its current version to go into your next commit snapshot.


      FilesStatus.png
  • Common Commands:

    • <font color=blue>Comiit:</font> Commit holds the current state of the repository. A commit is also named by SHA1 hash. You can consider a commit object as a node of the linked list. Every commit object has a pointer to the parent commit object. From a given commit, you can traverse back by looking at the parent pointer to view the history of the commit. If a commit has multiple parent commits, then that particular commit has been created by merging two branches.
    • <font color=blue>Branch:</font> Branches are used to create another line of development. By default, Git has a master branch, which is same as trunk in Subversion. Usually, a branch is created to work on a new feature. Once the feature is completed, it is merged back with the master branch and we delete the branch. Every branch is referenced by HEAD, which points to the latest commit in the branch. Whenever you make a commit, HEAD is updated with the latest commit.
    • <font color=blue>Tag:</font> Tag assigns a meaningful name with a specific version in the repository. Tags are very similar to branches, but the difference is that tags are immutable. It means, tag is a branch, which nobody intends to modify. Once a tag is created for a particular commit, even if you create a new commit, it will not be updated. Usually, developers create tags for product releases.
    • <font color=blue>Clone:</font> Clone operation creates the instance of the repository. Clone operation not only checks out the working copy, but it also mirrors the complete repository. Users can perform many operations with this local repository. The only time networking gets involved is when the repository instances are being synchronized.
    • <font color=blue>Pull:</font> Pull operation copies the changes from a remote repository instance to a local one. The pull operation is used for synchronization between two repository instances. This is same as the update operation in Subversion.
    • <font color=blue>Push:</font> Push operation copies changes from a local repository instance to a remote one. This is used to store the changes permanently into the Git repository. This is same as the commit operation in Subversion.
    • <font color=blue>Head:</font> HEAD is a pointer, which always points to the latest commit in the branch. Whenever you make a commit, HEAD is updated with the latest commit. The heads of the branches are stored in .git/refs/heads/directory.

2.The Workflow of Github:

<font color="red">Step 1:</font> Getting a Git Repository

  1. You can take a local directory that is currently not under version control, and turn it into Git repository.

    Initializing a Repository in an Existing Directory:

    Input Command: cd path, the path is project directory that is currently not under version control and you want to start controlling it with Git.

    Input Command: git init, it creates a new subdirectory named .git that contains all of your necessary repository files — a Git repository skeleton.

  2. You can clone an existing Git repository from elsewhere.

    Cloning an Existing Repository:

    If you want to get a copy of an existing Git repository — for example, a project you’d like to contribute to — the command you need is git clone <url>. Such is like this:

    git clone https://github.com/MikoyChinese/HelloWorld
    

<font color="red">Step 2:</font> Recording Changes to the Repository

  1. Checking the Status of Your Files:

    The main tool you use to determine which files are in which state is the git status. It looks like this:

    $ git status
    On branch master
    Your branch is ahead of 'origin/master' by 6 commits.
      (use "git push" to publish your local commits)
    nothing to commit, working directory
    

    If you creat a new file(eg: README.md), and you run git status, you will see your untracked file like so:

    $ git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        README.md
    
    nothing added to commit but untracked files present (use "git add" to track)
    
  2. Tracking New Files:

    In order to begin tracking a new file, you use the command git add. To begin tracking the README file, you can run this:

    git add <files>
    

    If you wanna tracking a new folder, you can use the <path> instead of the <files>. Then, that will track all the files in your folder path.

  3. Staging Modified Files:

    If you change a file(eg: README.md) that was already tracked and then you run git status command, you get something that looks like this:

    $ git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        new file:   README
    
    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.md
    

    To stage it, you run the git add <file> command. git add is a multipurpose command — you use it to begin tracking new files, to stage files, and to do other things like marking merge-conflicted files as resolved. And then you run git status command, you will see like this:

    $ git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    Changes to be committed:
     (use "git reset HEAD <file>..." to unstage)
    
       new file:   README.md
       modified:   README.md
    
  4. Ignoring Files:

    Often, you’ll have a class of files that you don’t want Git to automatically add or even show you as being untracked. These are generally automatically generated files such as log files or files produced by your build system. In such cases, you can create a file listing patterns to match them named .gitignore. Here is an example .gitignore file:

    # ignore all .a files
    *.a
    
    # but do track lib.a, even though you're ignoring .a files above
    !lib.a
    
    # only ignore the TODO file in the current directory, not subdir/TODO
    /TODO
    
    # ignore all files in the build/ directory
    build/
    
    # ignore doc/notes.txt, but not doc/server/arch.txt
    doc/*.txt
    
    # ignore all .pdf files in the doc/ directory and any of its subdirectories
    doc/**/*.pdf    
    
  5. Committing Your Changes:

    Now that your staging area is set up the way you want it, you can commit your changes.

    $ git commit
    

    Git creates your commit with that commit message (with the comments and diff stripped out). The editor displays the following text (this example is a Vim screen):

    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    # On branch master
    # Your branch is up-to-date with 'origin/master'.
    #
    # Changes to be committed:
    #   new file:   README
    #   modified:   CONTRIBUTING.md
    #
    ~
    ~
    ~
    ".git/COMMIT_EDITMSG" 9L, 283C
    

    Alternatively, you can type your commit message inline with the commit command by specifying it after a -m flag, like this:

    git commit -m <Your commit message.>
    
  6. Removing the Files:

    If you want to remove a file which is from your tracked files from git or in other words, remove it from your staging area. The git rm <file> does do that, and it means that it will remove the file from your working directory, and you can't see it anymore as an untracked file next time around. Then, if you run git rm, it stages the file’s removal(such as README.md):

    $ git rm README.md
    rm 'README..md'
    $ git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
    deleted:    README.md
    
    

    If you want to keep the file in your working directory but remove it from your staging area. In other words, you wanna keep the file on your hard drive but not have git track it anymore. You can use the git rm <file> with --cached flag.

    git rm --cached <file>
    
    
  7. Moving Files:

    Unlike many other VCS systems, Git doesn’t explicitly track file movement. If you rename a file in Git, no metadata is stored in Git that tells it you renamed the file. However, Git is pretty smart about figuring that out after the fact — we’ll deal with detecting file movement a bit later.
    If you want to rename a file in git, you can run the command something like this:

    git mv <file_frome/old_filename> <file_to/new_filename>
    

    However, this is equivalent to running something like this:

    $ mv <file_from> <file_to>
    $ git rm <file_from>
    $ git add <file_to>
    

<font color="red">Step 3:</font> Working with Remotes on Github

  1. Configure your git remote:

    If you are the first time to use the git's remote function, you should configure your git such like this:
        git config --global user.name <Your Name>
        git config --global user.email <Your Email>
    
  2. Showing Your Remotes:

    If you wanna show what remote your git repositories have, you can run git remote -v which shows you the URLs that Git has stored for the shortname to be used when reading and writing to that remote.
  3. Adding Remote Repositories:

    There are so many demonstrations which show you add remote called origin. But in fact, you would need add remote with yourself shortname you like.So you also can add remotes like this:
    git add remote <Shortname> <Repository_url>
    
  4. Pushing to Your Remotes:

    When you finished your project and you wanna share it to github, you have to push it upstream. The command for this is very simple: git push <remote> <branch>, meaning that it will push your <remote> project upstream to <branch>. If you want to push your master branch to origin server, you can run like this:
    git push origin master
    
  5. Checkout You branch:
    Sometime we want to work with other and noninterference, so that you can creat another branch to save new project coping from origin project, and another worker can checkout their branch to hisself work. The eg is like this:
    git checkout <new_branch>
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容