最近在学习Git,我先后在CentOS6.4, Ubuntu12.04, Windows7上安装Git,遇到的问题比较多的是在CentOS上的安装,Ubuntu和Windows7上的安装相对比较简单,只需按照Git官网上的的tutorial操作即可,本文就Git在CentOS6.4上的安装进行说明,并列举了基本的Git操作。

@1:Install & Configure

&1:Install

#If you’re on Fedora, you can use yum:
$ yum install git-core
#Or if you’re on a Debian-based distribution like Ubuntu, try apt-get:
$ apt-get install git

  上面的这种方式根据所使用的系统的版本号不同,可能安装的并不是最新版的Git【例如我的系统是CentOS6.4,使用yum install git得到的Git版本是1.7.1,所以在下面的commit环节遇到了麻烦的问题,网上提示要安装1.7.10以上的版本】,所以可以采用下面的方法:

  首先到https://github.com/git/git上,下载最新版本的git源文件。在进行安装前,需要先安装: expat,curl, zlib, 和 openssl;除了expat 外,其它的可能在你的机器上都安装了。安装expat:

$ yum install expat-devel

然后就可以安装了:

$ make prefix=/usr all   # 以当前用户权限运行
$ make prefix=/usr install # 以root权限运行

&2:Configure(Just for once.只需进行一次配置即可)

$ git config --global user.name "Your Name Here"
# Sets the default name for git to use when you commit
$ git config --global user.email "your_email@example.com"
# Sets the default email for git to use when you commit
$ git config --global push.default simple
# set the push.default

  其中配置git config --global push.default simple的原因是,Git2.0版本中将push的默认值改成了'simple'(Git1.x版本中,该值的默认值为'matching'),matching 意味着如果在进行push时没有指定branch,则默认push所有的本地branch到远程Repo;simple意味着如果在进行push时没有指定branch,则默认只push当前的branch。

@2:Create A Repo

&1:Create A Repo on Github,e.g. Hello_World Repo.

&2:Create a README for your repository

【README虽然不是必需的,但最好还是要有README文件的,该文件可以用来对Project进行简要的说明:如何进行安装,必要的操作说明以及作者的联系方式等】

*1:Create the README file

$ mkdir ~/Hello-World
$ cd ~/Hello-World
$ git init
# Sets up the necessary Git files.Initialized empty Git repository in Hello-World/.git/
$ touch README

  其中,git init是告诉Git当前目录是我们需要跟踪的项目。

*2:Commit your README

$ git add README
# Stages your README file, adding it to the list of files to be committed
$ git commit -m 'first commit'
# Commits your files, adding the message "first commit"

  如果要stage当前目录(.)下的所有的文件和文件夹(以及其子文件夹),可以使用下面的命令:

$ git add .

  如果你有需要检查你现在的已加载(staged)和未加载(unstaged)文件的状态、提交等,你可以询问git的状态:  

$ git status

*3:Push your commit

  So far, everything you've done has been in your local repository, meaning you still haven't done anything on GitHub yet. To connect your local repository to your GitHub account, you will need to set a remote for your repository and push your commits to it.

$ git remote add origin https://github.com/username/Hello-World.git
# Creates a remote named "origin" pointing at your GitHub repository. We can use any other name instead of "orgion"
$ git push origin master
# Sends your commits in the "master" branch to GitHub

@3: Update

  如果想从远程代码库上取得它最新的版本,切换(cd)到项目的根目录下,然后执行:

$ git pull origin master

@4:Fork A Repo

&1:Click the "Fork" button in the Github.com repository to fork the repository. e.g."Spoon-Knife"

&2:After the former step,you've successfully forked the repository, but it only exists on GitHub. To be able to work on the project,you will need to clone it to your local machine.

$ git clone https://github.com/username/Spoon-Knife.git
# Clones your fork of the repository into the current directory.

&3:Configure remotes

  When a repository is cloned, it has a default remote called origin that points to your fork on GitHub, not the original repository it was forked from. To keep track of the original repository, you need to add another remote named upstream:

$ cd Spoon-Knife
$ git remote add upstream https://github.com/octocat/Spoon-Knife.git
# Assigns the original repository to a remote called "upstream"
$ git fetch upstream
# Pulls in changes not present in your local repository, without modifying your files

&4:Pull in upstream changes

If the original repository gets updated, you can add those updates to your fork:

$ git fetch upstream
# Fetches any new changes from the original repository
$ git merge upstream/master
# Merges any changes fetched into your working files

&5:Delete your fork

To delete a fork, just follow the same steps as you would to delete a regular repository:
[Select "Settings" from the repository action bar]->[Click Delete this repository in the Danger Zone™ area]->[Read the warnings]->[Enter the name of the repository you want to delete]->[Click I understand the consequences, delete this repository]
NOTE:Deleting a private repository also deletes all of its forks. Deleting a public repository will not.

@5:About Branches 

&1:建立分支

  建立分支是指创建代码的独立版本,独立于你的master分支(主干分支)。默认地,每次你提交到Git的文件都会被储存到master分支。
  当我们想要向项目里添加一个功能,但我们希望能够回滚到现在版本,以防出现差错,或者我们可以随时决定要放弃这个功能,我们就可以通过创建分支来实现这些。
  创建并同时切换到新建的分支:  

$ git checkout -b new_feature

  或者
  先创建一个分支然后手动切换:  

$ git branch new_feature
$ git checkout new_feature

  要查看当前项目下所有的分支:  

$ git branch

  通过创建分支,我们可以在项目上无所顾忌地做任何想做的,因为任何时候,我们都可以回到创建分支前的状态。并且,我们同时可以有多个分支,甚至可以在一个分支上再创建一个分支。

&2:合并分支

  当我们对在分支(比如前面提到的new_feature分支)中做的新功能满意了的时候,我们想要把它加到master分支上。此时我们可以首先切换到new_feature分支,然后进行stage并且commit:

$ git add .
$ git commit -m "adds my new feature"

  然后切换到master分支:

$ git checkout master

  像这样进行合并:

$ git merge new_feature

  此时,你的master分支和你的新功能分支会变成一样的了。

&3:删除分支

$ git branch -d new_feature

  若修改已经合并了,则它只会删除分支。若分支没有合并,则会得到一个错误信息。可以通过下面的方法来删除一个未合并的分支(通常你不想保留的修改):

$ git branch -D new_feature

  你需要发送一样的命令附带一个大写D。意思是“强制删除分支,无论如何我不想要它了。”

&4:回滚

  在某些时候,我们可能想要回到之前的代码版本。查看所有的已完成的提交:

$ git log

  这会输出提交的历史记录:

commit ca82a6dff817ec66f44342007202690a93763949Author: your_username your_email@domain.comDate: Mon Nov 4 12:52:11 2013 -0700 changes the frontpage layout
commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7Author: your_username your_email@domain.comDate: Mon Nov 4 11:40:33 2013 -0700 adds my new feature
commit a11bef06a3f659402fe7563abf99ad00de2209e6Author: your_username your_email@domain.comDate: Mon Nov 4 10:37:28 2013 -0700 initial commit
  假如此时想回到“adds my new feature”这个提交,简单地用提交的ID做checkout(通常只用到ID开头的9个字符)

$ git checkout 085bb3bcb

  也可以签出到一个新的分支,像这样:

$ git checkout -b my_previous_version 085bb3bcb

  只是别太疯狂了!分支越复杂,就越难确定你真正在做什么。

&5:分支的Demo:

lxw@ubuntu:~/Documents/ShellScript/DealScript$ git branch
* master
version1
lxw@ubuntu:~/Documents/ShellScript/DealScript$ ls
apnic.txt awkDealScript.sh newAWKDealScript.sh
output README.md
lxw@ubuntu:~/Documents/ShellScript/DealScript$ git checkout version1
Switched to branch 'version1'
lxw@ubuntu:~/Documents/ShellScript/DealScript$ ls
apnic.txt awkDealScript.sh
README.md
lxw@ubuntu:~/Documents/ShellScript/DealScript$ git branch
master
* version1
lxw@ubuntu:~/Documents/ShellScript/DealScript$

  分支不同所处的环境不同.

@6:其他命令:

#1: git remote/git remote -v

#2: git remote rm remote_name

#3: git ls-files

#4: git 删除远程分支: git push --delete origin branchName

#5: git 查看所有分支(本地分支+远程分支): git branch -a

#6: 使用git 部署代码,git branch -a 里面列出的很多远程的分支,其实都是已经被删除了的。可在git pull,他们仍旧是存在,如何删除这样的缓存?

git remote prune origin
or
git fetch -p

References:

Git Tutorial:http://www.ralfebert.de/tutorials/git/

Git 2.0 changes push default to 'simple': http://blog.nicoschuele.com/posts/git-2-0-changes-push-default-to-simple

15分钟学会使用Git和远程代码库: http://blog.jobbole.com/53573/

Way to Git的更多相关文章

  1. Git 子模块 - submodule

    有种情况我们经常会遇到:某个工作中的项目需要包含并使用另一个项目. 也许是第三方库,或者你 独立开发的,用于多个父项目的库. 现在问题来了:你想要把它们当做两个独立的项目,同时又想在 一个项目中使用另 ...

  2. Git 在团队中的最佳实践--如何正确使用Git Flow

    我们已经从SVN 切换到Git很多年了,现在几乎所有的项目都在使用Github管理, 本篇文章讲一下为什么使用Git, 以及如何在团队中正确使用. Git的优点 Git的优点很多,但是这里只列出我认为 ...

  3. Git与Repo入门

    版本控制 版本控制是什么已不用在说了,就是记录我们对文件.目录或工程等的修改历史,方便查看更改历史,备份以便恢复以前的版本,多人协作... 一.原始版本控制 最原始的版本控制是纯手工的版本控制:修改文 ...

  4. Git Bash的一些命令和配置

    查看git版本号: git --version 如果是第一次使用Git,你需要设置署名和邮箱: $ git config --global user.name "用户名" $ gi ...

  5. 在Ubuntu 16.10 安装 git 并上传代码至 git.oschina.net

    1. 注册一个账号和创建项目 先在git.oschina.net上注册一个账号和新建一个project ,如project name 是"myTest". 2.安装git sudo ...

  6. 史上最详细git教程

    题外话 虽然这个标题很惊悚,不过还是把你骗进来了,哈哈-各位看官不要着急,耐心往下看 Git是什么 Git是目前世界上最先进的分布式版本控制系统. SVN与Git的最主要的区别 SVN是集中式版本控制 ...

  7. [版本控制之道] Git 常用的命令总结(欢迎收藏备用)

    坚持每天学习,坚持每天复习,技术永远学不完,自己永远要前进 总结日常开发生产中常用的Git版本控制命令 ------------------------------main-------------- ...

  8. 【解决方案】Myeclipse 10 安装 GIT 插件 集成 步骤 图解

    工程开发中,往往要使用到集成GIT ,那么下面说说插件安装步骤 PS:以Myeclipse 10 为例,讲解集成安装步骤. ----------------------main------------ ...

  9. git 命令

    切换仓库地址: git remote set-url origin xxx.git切换分支:git checkout name撤销修改:git checkout -- file删除文件:git rm  ...

  10. git亲测命令

    一.Git新建本地分支与远程分支关联问题 git checkout -b branch_name origin/branch_name 或者 git branch --set-upstream bra ...

随机推荐

  1. Windows GDI 映射模式(出自:Windows程序设计第5版-珍藏版)

    GDI映射模式(mapping mode):和映射模式紧密相关的还有4个其它的设备环境属性:1.窗口原点(window origin)2.视口原点(viewport origin)3.窗口范围(win ...

  2. php使用N层加密eval gzinflate str_rot13 base64 破解方法汇总

    php使用N层加密eval gzinflate str_rot13 base64 破解方法汇总 来源:本站转载 作者:佚名 时间:2011-02-14 TAG: 我要投稿 PHP使用eval(gzin ...

  3. js使用正则表达式验证身份证格式

    function checkIdentity(identity){ var reg = /^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$/; i ...

  4. Swoole系列(一):简介

    前言: 实际上作为一名PHP程序员,我很清楚PHP的确有很多局限性,比如Unix系统编程.网络通信编程.异步io,大部分PHPer不懂.PHP界也确实没有这样的东西.Swoole开源项目就是为了弥补P ...

  5. 如何查询当前手机的cpu架构,so库导入工程又出异常了?

    执行adb命令: adb shell cat /proc/cpuinfo 对应文件夹 AArch64 == arm64-v8a ARMv7 == armeabi-v7a ............等 其 ...

  6. 第一百七十四节,jQuery,Ajax进阶

    jQuery,Ajax进阶 学习要点: 1.加载请求 2.错误处理 3.请求全局事件 4.JSON 和 JSONP 5.jqXHR 对象 在 Ajax 课程中,我们了解了最基本的异步处理方式.本章,我 ...

  7. Eclipse 重启选项

    重启 Eclipse 重启选项允许用户重启 Eclipse. 我们可以通过点击 File 菜单选择 Restart 菜单项来重启 Eclipse. 在安装插件后,用户一般都会被提醒要重启 Eclips ...

  8. python 面试题 string int

    str1 = 'hello' str2 = str1 str3 = str1 str4 = str1 str1 = '' int1 = 1 int2 = int1 int3 = int1 int4 = ...

  9. Android WebView ScrollBar设置

    WebView wv; wv.setVerticalScrollBarEnabled(false);  取消Vertical ScrollBar显示 wv.setHorizontalScrollBar ...

  10. Win7系统安装 MySQL 5.7.23

    1. 下载 MySQL 5.7版本:https://dev.mysql.com/downloads/mysql/5.7.html#downloads 2. 解压到指定文件夹,mysql根目录下创建my ...