• First thing first, you can easily install git in all 3 mainstream OS, Windows, Linux, OSX.

    Get windows installer on https://git-for-windows.github.io/.

    Note that in Windows, the official git tool was called "git for windows" as well as msysgit. They're aimed at the same thing.

  • Git provide a command tool named git config for environment configuration.    

    You should configure some basic information at least for better using experience.

    Editor & Merge tool will be need other stand alone tools, e.g. gvim, kdiff3, etc.

Here we do not configure external editor but get the kdiff3 right here http://sourceforge.net/projects/kdiff3/files/latest/download?source=files.

  • User Information

    $ git config --global user.name "John Doe"

    $ git config --global user.email johndoe@example.com

  • Editor

    $ git config --global core.editor gvim

  • Merge tool

    Say we're using kdiff3 in windows for Comparison and merge branch.

    $ git config --global merge.tool kdiff

    $ git config --global mergetool.kdiff3.path "your path to kdiff3.exe"

  • PS: the configuration could be list using below commands:

    $ git config --list

    Or if you need to check the value of a configuration, use:

    $ git config user.name

  • As sometimes you will need to clone a remote repository, and you're trying to use the SSH way for remote operations. It's time for some SSH stuffs.
    • Check whether your ssh keys exists:

      $ ls ~/.ssh

      There should be text files similar to below, which contains the public key & end with your email.

      id_dsa.pub

      id_rsa.pub

    • Generate your new ssh keys:

      $ ssh-keygen -t rsa -b 4096 -C "your-email@example.com"

    • Ensure ssh-agent is enabled:

      # start the ssh-agent in the background
      $ ssh-agent -s
      # Agent pid 59566

    • Add your SSH key to the ssh-agent:

      $ ssh-add ~/.ssh/id_rsa

    • Add your SSH key to your account, copy the whole public key and paste to your remote Git repository server such as GitHub, GitLab, etc.

      $ clip < ~/.ssh/id_rsa.pub

    • PS: This ssh way using your private key to identify your user information, it was used when the repository URL happened to be something similar to git://example.com/repo.git.

Dance at local

  • Initialize a new git repository or clone a remote repository
    • Initialize locally

      Change directory to your local project and then:

      $ git init

      $ git add *.cpp

      $ git add README

      $ git commit -m 'initial project version'

    • Clone repository

      $ git clone git://github.com/example/project.git [directory name]

      This will create a directory using your [directory name] or the remote project name (If no directory name specified).

  • Git status

    There're 3 different status here includes changed, staged, committed.

    • Untracked

      By default, if you create, modified some files in your local repository, these files will get a untracked status, nothing is record in Stage Area.

      $ gvim README

      $ git status

      # On branch master

      # Untracked files:

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

      #

      # README

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

    • Stage

      Now you're going to track & stage these files, you can use git add to finish this.

      These files will be committed together when you commit your changes next time.

      $ git add README

      $ git status

      # On branch master

      # Changes to be committed:

      # (use "git reset HEAD <file>..." to unstage)

      #

      # new file: README

      Note that you can use git diff to compare your changes all the time.

      $ git diff

      This will compare your unstaged file with your staged area.

    • Commit

      You're ready to finish a period of development, now let's get a version clean and ready.

      $ git diff --staged

      This will compare your staged changes with the last time you committed.

      $ git commit -m "Example message"

      If all the needed files were tracked already, then you can also use below command to commit your changes directly without Stage files:

      $ git commit -a -m "Example message"

  • Remove files
    • To remove files from git, you'll need command to complete this task. This is because your file records were in the staged area, and this command will help you to remove it from the Stage Area.

      After deleting files, the status will become "Changed but not updated".

    • Then you can use:

      $ git rm filesToBeRemoved

    • To just untrack some files but not remove them, you can use:

      $ git rm --cached filesToBeUntracked

      After that, you can just add the missing rule to the .gitignore file.

    • Glob could also be used, note the \ symbol will help to achieve this feature recursively:

      $ git rm \*.log

  • Rename files
    • Using git mv

      $ git mv filename1 filename2

    • Using git rm & git add, this is important when you're renamed some files already but haven't yet remove the records in git stage area.

      $ mv README.txt README

      $ git rm README.txt

      $ git add README

  • Amend the last commit
    • Using --amend param, the last commit will be together with the new operations.

      $ git commit -m 'initial commit'

      $ git add forgotten_file

      $ git commit --amend

    • Unstage file & Discard changes:

      $ git reset HEAD benchmarks.rb

      # Changes to be committed:

      # (use "git reset HEAD <file>..." to unstage)

      #

      # modified: README.txt

      #

      # Changed but not updated:

      # (use "git add <file>..." to update what will be committed)

      # (use "git checkout -- <file>..." to discard changes in working directory)

      #

      # modified: benchmarks.rb

      #

      Firstly the file benchmarks.rb was to be committed just like the README.txt above, but now it become a unstaged file which needed other steps.

    • You can add it back to staged status or checkout to discard the changes.

      $ git checkout -- benchmarks.rb

      However, this is a dangerous command because it will really discard your changes, unlike some more friendly commands, i.e. stashing.

Remote repository

  • Show me the verbose

    $ git remote -v

    #origin git://example.com/project.git

    #peter git://hello.com/project.git

    #lan //dell-pc/d/path/project.git

  • Add remote repository
    • Using git remote add to configure a new repository.

      $ git remote add alias git://github.com/paulboone/ticgit.git

      Once you have your remote repositories, you can specify you'll choose which repository to fetch data:

      $ git fetch peter

      Unpacking objects: 100% (44/44), done.

      From git://github.com/paulboone/ticgit

      * [new branch] master -> peter/master

      * [new branch] ticgit -> pter/ticgit

  • Push local commits to remote repository
    • This is a straight forward command, if the branch doesn't exist in remote, the new branch will be created automatically:

      $ git push [remote-name] [branch-name]

Branch Model

This is the most widely used part in git. Efficient, Clear, and works great.

  • Create a new branch

    $ git branch testing

    • In your local, there's always a HEAD pointer point to a specified version of your working branch. That is:

    • As the image says, master & testing were the two branches exist. HEAD now point to master. Because git branch will not change the working branch anyway.
    • Now you're going to check out the new branch for working:

      $ git checkout testing

      The HEAD pointer has been redirected. And now the checkout command was used to switch branches, instead of discard changes.

    • After switch and commit different things in both branches, they might look like:

      This model explains why we should use branch to control our development, and how easily it is to use.

  • Basic workflow of development based on git
  1. Firstly you've decided to work on a new feature or a bug fix, say #iss53.

    $ git branch iss53

    $ git checkout iss53

  • Now you have below model look like:

  1. Some work have been done, the progress moved on:

    $ git commit -m "Add something"

  2. But there're some urgent fix should be done now, you'll switch back to complete the hotfix:
  • $ git checkout master

  • $ git branch hotfix

    $ git checkout hotfix

    #Switched to a new branch "hotfix"

    When the hotfix has been committed, it looks like:

  1. Now we merge the hotfix to master and push it to remote server:

  2. Things were moving smoothly and the hotfix can be deleted safely, and the iss53 could be keep developing again.

    $ git branch -d hotfix

    #Deleted branch hotfix (3a0874c).

    $ git checkout iss53

    $ git commit -m "other features"

  3. When the iss53 has been finished, just merge the iss53 into master:

    $ git checkout master

    $ git merge iss53

    Even though the process of merge was different from the hotfix one, the git decided how to merge these two branches itself. Finding the Common ancestor, merge the commits, and create a new commit (which is a special commit named "Merge commit").

  • What if there're conflict occurred when merging? At the moment, all the conflict files will be listed as "Unmerged files".

    $ git mergetool

    merge tool candidates: kdiff3 tkdiff xxdiff meld gvimdiff opendiff emerge vimdiff

    Merging the files: index.html

    Normal merge conflict for 'index.html':

    {local}: modified

    {remote}: modified

    Hit return to start merge resolution tool (opendiff):

    After that, the status will become modified again and could be committed now.

  1. Actually, you can also merge the master branch into your #iss53 anytime, if other's important changes are needed. And then push the local branch to the remote repository, create a merge request. Waiting for someone who has a write access right to merge your branch into master. This is an acceptable workflow as well.

    $ git checkout iss53

    $ git fetch origin

    $ git merge origin/master iss53

    $ git push origin iss53

    This kind of process could be completed easier via rebase:

    $ git checkout iss53

    $ git rebase master

    $ git push origin iss53

    The rebase command will generate a clearer development history:

  • Tracking branch
    • A local branch checkout from remote server was called a tracking branch.

      $ git checkout -b localfix origin/serverfix

      Branch localfix set up to track remote branch refs/remotes/origin/serverfix.

      Switched to a new branch "localfix "

      The branch name can be the same as branch on server side.

      $ git push origin serverfix:localfix or

      $ git push origin serverfix (if the name were the same)

Begin using git的更多相关文章

  1. Begin using git (Part1) - Git的安装与配置

    Git提供了适用于Linux, Windows, OSX的客户端, 本节以Windows为例介绍基本安装与配置. 所需工具:msysgit, kdiff3. Get windows installer ...

  2. Git Shell 安装版本

    #!/bin/sh v1.; do echo "Begin install Git $ver."; git reset --hard git clean -fdx git chec ...

  3. 第七章 : Git 介绍 (下)[Learn Android Studio 汉化教程]

    Learn Android Studio 汉化教程 Let’s reset even further to remove all traces of your work on the deprecat ...

  4. HDU 4585 Shaolin (STL)

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  5. 记一次git amend事故处理方案

    一.问题回顾 问题是git commit --amend 引起的. 一条commit已经push到远端develop了,但是后来又在这条commit上进行了amend操作,导致这条commit的哈希码 ...

  6. Git使用出错:Couldn‘t reserve space for cygwin‘s heap, Win32

    今天使用Git在命令行下更新代码遇到了问题,起初觉得是自己安装某软件导致冲突,从网上搜索了一下找到类似问题,成功解决问题. 错误信息如下: E:\storm-sql>git pull origi ...

  7. configure Git to accept a particular self-signed server certificate for a particular https remote

    get the self signed certificate put it into some (e.g. ~/git-certs/cert.pem) file set git to trust t ...

  8. Git Learning - By reading ProGit

    Today I begin to learn to use Git. I learn from Pro Git. And I recommend it which is an excellent bo ...

  9. 利用git+hugo+markdown 搭建一个静态网站

    利用git+hugo+markdown 搭建一个静态网站 一直想要有一个自己的文档管理系统: 可以很方便书写,而且相应的文档很容易被分享 很方便的存储.管理.历史记录 比较方面的浏览和查询 第一点用M ...

随机推荐

  1. 【shell】while与until循环

    while循环 #!/bin/bash i=1 s=0 while [ $i -le 100 ] do s=$(($s+$i)) ##变量运算 i=$(($i+1)) done echo " ...

  2. [转]开源那些事儿(四)-如何使用CodePlex进行项目管理

    本文版权信息作者:Jake Lin(Jake's Blog on 博客园) 出处:http://www.cnblogs.com/procoder/archive/2010/02/10/About-Op ...

  3. 基础:从概念理解Lucene的Index(索引)文档模型

    转:http://blog.csdn.net/duck_genuine/article/details/6053430   目录(?)[+]   Lucene主要有两种文档模型:Document和Fi ...

  4. Redis服务停止报错解决方案[NOAUTH Authentication required]

    Redis服务器设置密码后,使用service redis stop 会出现以下信息: service redis stop Stopping ... OK (error) NOAUTH Authen ...

  5. IntelliJ IDEA以不同格式导出数据库的数据

    在数据表内容上点击右键,弹出窗口中先选择Data Extractor SQL Inserts,二级菜单会列出导出数据的类型,这里选择SQL Inserts 然后选择Dump Data菜单中的To Fi ...

  6. IIS SMTP status codes

    Here are the meaning of SMTP status codes. Status Code Description 211 System status, or system help ...

  7. SVN 分支及合并的介绍和实践---命令行

    写在前面 一些相关的概念和原理 进行分支开发的最佳实践 合并的分类 在 Eclipse 中进行合并操作 相关资源 写在前面 本文是由演讲整理而来的,介绍了 SVN 分支与合并的概念.流程和一些实际操作 ...

  8. title与h1标签的区别和联系

    很多新站长在网站SEO过程中,会认为把H1等同于Title. 其实两是有区别和联系的,两者不能划等号.下面主要从文章和页面角度分析title和H1. H1等同于title吗? H1不等于title.H ...

  9. PLSQL_闪回操作2_Fashback Version Query

    2014-12-09 Created By BaoXinjian

  10. centos6.5安装mysql记录

    1.查看操作系统相关信息. [root@linuxidc ~]# cat /etc/issue CentOS release 6.5 (Final) Kernel \r on an \m [root@ ...