git 基本操作

来源:互联网 发布:java核心技术卷1 编辑:程序博客网 时间:2024/06/04 19:15
【参考资料】
https://git-scm.com/book/zh/v2
http://blog.jobbole.com/78960/
http://www.bootcss.com/p/git-guide/


【常用命令】
  git help
  git init :  初始化目录
  git add file_name : 添加
  git commit -m ""  : 提交
  git log : 查看修改
  git status : 查看状态
  git status -s  查看状态+紧凑格式
  rm file_name: 直接删除即可
  git push origin master : 推送本地到 repository
  git push -u origin master


【安装】
一、安装Git
【Mac】http://brew.sh/,先装包管理工具Homebrew,再用brew install git
【Linux】一般的发行版都自带Git,yum -y git
Eclipse、Android Studio和XCode都各自带有GUI。此外,推荐使用SourceTree GUI工具,结合使用BeyondCompare进行对比合并。

二、基本配置
git config --global user.name fengzhiping
git config --global user.email fengzhiping@baidu.com
git config —list 查看配置信息


2.1 自动补全
参考:https://git-scm.com/book/zh/v1/Git-基础-技巧和窍门
curl -s http://svn.baidu.com/downloads/git-completion.bash >~/.git-completion.bash
source ~/.git-completion.bash

2.2 Git命令别名
参考http://blog.csdn.net/u010205879/article/details/46580645
git config --global alias.st status // 把status => st
git config --global alias.co checkout // checkout => co
git config --global alias.ci commit // commit => ci
git config --global alias.br branch // branch => br
git config --global alias.unstage reset HEAD // reset HEAD => unstage
git config --global alias.last log -1 // 显示最后一次提交信息

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit" // 让git 显示日志时,不同区域显示不同的颜色。

git config --global alias.tree 'log --graph --decorate --pretty=oneline --abbrev-commit' //图形化显示log


三、基本操作
1、克隆项目代码 (clone)
git clone  xxxx

2、本地添加文件 & 提交到本地(add & commit)
git add -u
git commit -m "fixed some bugs"

其中:
add     命令将修改加到暂存区
-u 只处理已经加入版本控制的文件   -a  选项是处理所有文件
commit 命令将缓冲区的修改提交到本地版本库

3、本地提交到中央仓库 (push)
git push origin master:master

origin是远端仓库的名字(默认行为,也可以使用其他名字,具体可以看看Git学习资料)。上面语句的意思是把本地master分支推送到远端master分支。

4、pull & fetch
fetch 操作是把远端仓库代码拉取到本地仓库,但是并不会影响本地工作空间的代码,也不会影响本地分支所指向的commit版本号。
pull  操作是把远端代码拉取到本地,并和工作区代码进行合并,会影响本地分支所指向的commit版本号,和远端同步。
pull = fetch +merge
git pull origin master:master     拉取远端master分支,并同步到本地master分支。

5、分支操作
本地分支
  • git branch             查看本地分支
    git branch name   新建分支
    git checkout  name 切换分支
    git checkout –b dev   创建分支并切换到dev分支等价于上两条命令
    git branch –d dev 删除dev分支
         以指定的远程分支创建分支:远程分支 svn/adp_7-4-0-0-togetherhi_BRANCH为例
       git checkout  -b svn/adp_7-4-0-0-togetherhi_BRANCH    origin/svn/adp_7-4-0-0-togetherhi_BRANCH

6、代码回滚 
方法一:
在分支上回滚,不建议在master上回滚代码,首先将当前分支备份,以免回滚后内容丢失 ,以下为回滚fenzhi1的例子
git branch fenzhi_bak  origin/fenzhi 
git reset --hard 6634735b199f6012899e676cb627196865523d05  回退到指定commit 
git push origin :fenzhi1    删除远程分支 
git push origin fenzhi1 新建立远程分支并把回退内容提交上去 


1 0
原创粉丝点击