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/
Way to Git的更多相关文章
- Git 子模块 - submodule
有种情况我们经常会遇到:某个工作中的项目需要包含并使用另一个项目. 也许是第三方库,或者你 独立开发的,用于多个父项目的库. 现在问题来了:你想要把它们当做两个独立的项目,同时又想在 一个项目中使用另 ...
- Git 在团队中的最佳实践--如何正确使用Git Flow
我们已经从SVN 切换到Git很多年了,现在几乎所有的项目都在使用Github管理, 本篇文章讲一下为什么使用Git, 以及如何在团队中正确使用. Git的优点 Git的优点很多,但是这里只列出我认为 ...
- Git与Repo入门
版本控制 版本控制是什么已不用在说了,就是记录我们对文件.目录或工程等的修改历史,方便查看更改历史,备份以便恢复以前的版本,多人协作... 一.原始版本控制 最原始的版本控制是纯手工的版本控制:修改文 ...
- Git Bash的一些命令和配置
查看git版本号: git --version 如果是第一次使用Git,你需要设置署名和邮箱: $ git config --global user.name "用户名" $ gi ...
- 在Ubuntu 16.10 安装 git 并上传代码至 git.oschina.net
1. 注册一个账号和创建项目 先在git.oschina.net上注册一个账号和新建一个project ,如project name 是"myTest". 2.安装git sudo ...
- 史上最详细git教程
题外话 虽然这个标题很惊悚,不过还是把你骗进来了,哈哈-各位看官不要着急,耐心往下看 Git是什么 Git是目前世界上最先进的分布式版本控制系统. SVN与Git的最主要的区别 SVN是集中式版本控制 ...
- [版本控制之道] Git 常用的命令总结(欢迎收藏备用)
坚持每天学习,坚持每天复习,技术永远学不完,自己永远要前进 总结日常开发生产中常用的Git版本控制命令 ------------------------------main-------------- ...
- 【解决方案】Myeclipse 10 安装 GIT 插件 集成 步骤 图解
工程开发中,往往要使用到集成GIT ,那么下面说说插件安装步骤 PS:以Myeclipse 10 为例,讲解集成安装步骤. ----------------------main------------ ...
- git 命令
切换仓库地址: git remote set-url origin xxx.git切换分支:git checkout name撤销修改:git checkout -- file删除文件:git rm ...
- git亲测命令
一.Git新建本地分支与远程分支关联问题 git checkout -b branch_name origin/branch_name 或者 git branch --set-upstream bra ...
随机推荐
- [na]esxi6.5的vmware安装
安装exsi6.5(最新的软件见评论区) ,安装 ,激活(可选) ,浏览器登录(.5好像没客户端了,所以就用浏览器,挺方便的) 安装包和key包在网盘里: 链接:https://pan.baidu.c ...
- java - day12 - InteraceTest
接口的实现.继承等 package test.interfacedemo; import test.interfacedemo.Inter; public class InterfaceDemo { ...
- log4j日志写入数据库
# log4j写入数据库 ### 前言-----------------------------log4j是写入日志到控制台和文件很常见,但是写入到数据库不多见.做性能测试写入到数据库,统计方便些. ...
- srping mvc学习
HOME 控制器 package ghy.webapp.myapp; import java.text.DateFormat; import java.util.Date; import java.u ...
- Python Numpy ValueError: data type must provide an itemsize
天朝网络锁国,百度找了半个小时找不出来原因,只能谷歌 谷歌第一条就是,顿时感觉幸福感来的太突然 原因是输入的矩阵均是字符串(从文件里读的) 那么就需要批量转数组,一行一行的转. 下面是我的代码: ro ...
- linux 分卷压缩命令
linux 分卷压缩命令 1.使用tar分卷压缩 格式 tar cvzf - filedir | split -d -b 50m - filename 样例: tar cvzf - ./picture ...
- 【ask】vmware(NAT)中的linux突然无法访问互联网网址,但是直接用ip可以访问。
前两天虚拟机里的linuxmint不知何故,突然无法访问互联网了.依稀记得是升级了win7下面的360安全卫士之后发生的事情.所以, 第1步就开始去找防火墙的各种设置,结果没有查到结果. 第2步猛然看 ...
- windows 32位以及64位的inline hook
Tips : 这篇文章的主题是x86及x64 windows系统下的inline hook实现部分. 32位inline hook 对于系统API的hook,windows 系统为了达成hotpatc ...
- linux下的shell 快捷键
Ctrl+p重复上一次命令Ctrl+a跳到第一个字符前Ctrl+x同上但再按一次会从新回到原位置Ctrl+b前移一个字符不删除字符情况下Ctrl+h删除前一个字符Ctrl+u删除提示符前的所有字符Ct ...
- java笔记十:java中的反射
Java中,反射是一种强大的工具.它使您能够创建灵活的代码,这些代码可以在运行时装配,无需在组件之间进行源代表链接.反射允许我们在编写与执行时,使我们的程序代码能够接入装载到JVM中的类的内部信息,而 ...