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(元數(shù)據(jù)) 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>
    
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末夹抗,一起剝皮案震驚了整個濱河市棠耕,隨后出現(xiàn)的幾起案子施绎,更是在濱河造成了極大的恐慌运杭,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件元咙,死亡現(xiàn)場離奇詭異赡译,居然都是意外死亡居扒,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進店門暂题,熙熙樓的掌柜王于貴愁眉苦臉地迎上來移剪,“玉大人,你說我怎么就攤上這事薪者∽菘粒” “怎么了?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵言津,是天一觀的道長攻人。 經(jīng)常有香客問我,道長悬槽,這世上最難降的妖魔是什么怀吻? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮初婆,結果婚禮上蓬坡,老公的妹妹穿的比我還像新娘。我一直安慰自己磅叛,他們只是感情好屑咳,可當我...
    茶點故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著弊琴,像睡著了一般兆龙。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上访雪,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天详瑞,我揣著相機與錄音掂林,去河邊找鬼。 笑死坝橡,一個胖子當著我的面吹牛泻帮,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播计寇,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼锣杂,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了番宁?” 一聲冷哼從身側響起元莫,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蝶押,沒想到半個月后踱蠢,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡棋电,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年茎截,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片赶盔。...
    茶點故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡企锌,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出于未,到底是詐尸還是另有隱情撕攒,我是刑警寧澤,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布烘浦,位于F島的核電站抖坪,受9級特大地震影響,放射性物質發(fā)生泄漏谎倔。R本人自食惡果不足惜柳击,卻給世界環(huán)境...
    茶點故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望片习。 院中可真熱鬧捌肴,春花似錦、人聲如沸藕咏。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽孽查。三九已至饥悴,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背西设。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工瓣铣, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人贷揽。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓棠笑,卻偏偏與公主長得像,于是被迫代替她去往敵國和親禽绪。 傳聞我的和親對象是個殘疾皇子蓖救,可洞房花燭夜當晚...
    茶點故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內(nèi)容