Begin using git

Installation and configuration

  • 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 [email protected]

  • 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 "[email protected]"

    • 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)

时间: 2024-10-06 00:38:47

Begin using git的相关文章

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

Git提供了适用于Linux, Windows, OSX的客户端, 本节以Windows为例介绍基本安装与配置. 所需工具:msysgit, kdiff3. Get windows installer on https://git-for-windows.github.io/ Get merge tool kdiff3 on http://sourceforge.net/projects/kdiff3/files/latest/download?source=files 在Windows Exp

What is Git? The 5 Minute Tutorial.

It’s been about one week since I started the iOS immersive class at The Flatiron School in NYC. To say I’ve learned a lot in 7 days is an understatement. The content, instructors, and classmates are all top-notch and I’m so grateful that I have this

python3中调用C语言的函数

一, 先用C语言写好一个函数库 #include<stdio.h> int add(int num1, int num2) { return num1 + num2; } int sub(int num1, int num2) { return num1 - num2; } int mul(int num1, int num2) { return num1 * num2; } int div(int num1, int num2) { return num1 / num2; } 二, 然后使用

我的git与github学习历程

因为想要知道如何把代码放到github上,所以就百度了一下,然后找到一个<如何从github上面拷贝源码>的文章,就先进行练习了下 1.首先到git官网下载git版本控制工具的安装包,下载好双击安装,所有的步骤我都默认的. git官网:http://git-scm.com/download/ 2.然后安装完成我把没打勾的地方都打勾了,然后点击完成就出现如下图蓝色网页和黑色弹框,蓝色网页的网址: file:///D:/Program%20Files/Git/ReleaseNotes.html 看

git进阶

git进阶 1 分支管理 分支就是科幻电影里面的平行宇宙,当你正在电脑前努力学习Git的时候,另一个你正在另一个平行宇宙里努力学习SVN. 如果两个平行宇宙互不干扰,那对现在的你也没啥影响.不过,在某个时间点,两个平行宇宙合并了,结果,你既学会了Git又学会了SVN! 分支在实际中有什么用呢?假设你准备开发一个新功能,但是需要两周才能完成,第一周你写了50%的代码,如果立刻提交,由于代码还没写完,不完整的代码库会导致别人不能干活了.如果等代码全部写完再一次提交,又存在丢失每天进度的巨大风险. 现

git workflows

https://www.atlassian.com/git/tutorials/comparing-workflows Comparing Workflows The array of possible workflows can make it hard to know where to begin when implementing Git in the workplace. This page provides a starting point by surveying the most

Git 基础

读完本章你就能上手使用 Git 了.本章将介绍几个最基本的,也是最常用的 Git 命令,以后绝大多数时间里用到的也就是这几个命令.读完本章,你就能初始化一个新的代码仓库,做一些适当配置:开始或停止跟踪某些文件:暂存或提交某些更新.我们还会展示如何让 Git 忽略某些文件,或是名称符合特定模式的文件:如何既快且容易地撤消犯下的小错误:如何浏览项目的更新历史,查看某两次更新之间的差异:以及如何从远程仓库拉数据下来或者推数据上去. 取得项目的 Git 仓库 有两种取得 Git 项目仓库的方法.第一种是

使用meld作为git的辅助工具

原文链接: https://lrita.github.io/2017/05/14/use-meld-as-git-tool/?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao.io 为什么使用meld 当同一个文件被多人编辑时,经常会出现冲突的问题,因此就需要解决冲突. 但是git内置的diff.merge工具比较孱弱,经常会发生一些问题,例如 删除的代码被人合并时又加了回来 删除的代码被人合并时又加了回来,我想这种场景使用g

git rebase -i

使用rebase -i会在终端出现一个交互页面. 在这个交互页面中我们可以对要rebase的commit做一定的修改. 用法 git rebase -i <master> 把当前的分支的commit放在<base>后面, -i会打开一个编辑器, 在这你可以为每一个commit输入一个命令, 这个命令决定了如何把单个的commit传输到new base. 还可以改变commit列表的顺序. 讨论 大多数开发者喜欢在merge一个分支到master的时候使用rebase -i打磨我们这