Way to Git

  最近在学习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/

原文地址:https://www.cnblogs.com/lxw0109/p/waytogit.html