前言:
首先了解一下git的是什么:
【百度百科解释】Git是一个开源的分布式版本控制系统,可以有效、高速的处理从很小到非常大的项目版本管理。[2] Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。
github是什么:
GitHub 是一个面向开源及私有软件项目的托管平台,因为只支持 Git 作为唯一的版本库格式进行托管,故名 GitHub。
注意 : gitHub 私有仓库是收费的,其同类产品“码云”,创建私有仓库是免费的。
1.安装
Linux 安装git
# yum -y install git-all
Windows 安装
https://git-scm.com/download/win
初次提交代码需要作如下设置
|
Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly:
git config --global user.name "Your Name" git config --global user.email you@example.com
If the identity used for this commit is wrong, you can fix it with:
git commit --amend --author=‘Your Name <you@example.com>‘ |
2.Git 仓库维护
查看分支 git branch
创建分支 git branch <name>
切换分支 git checkout <name>
创建+切换分支 git checkout -b <new branch>
合并到当前分支 git merge <other branch name>
删除本地当前分支 git branch -D <current branch name>
删除远程分支 git push origin :test
3.代码提交与合并
3.1 commit and push
git add .
git commit -m "取消slave集群数据插入"
git pull --rebase origin master
如果发生冲突,修改冲突文件后
git add ./
git rebase --continue
git push origin caixisheng
3.2 by bash
例: 将分支 cai 合并到主分支master
$ git checkout master
$ git merge cai
$ git push origin master
3.3 stash
$Git stash 可用来暂存当前正在进行的工作, 比如想pull 最新代码, 又不想加新commit, 或者另外一种情况,为了fix 一个紧急的bug, 先stash, 使返回到自己上一个commit, 改完bug之后再stash pop, 继续原来的工作。
基础命令:
$git stash
$do some work
$git stash pop
进阶:
当你多次使用’git stash’命令后,你的栈里将充满了未提交的代码,这时候你会对将哪个版本应用回来有些困惑,’git stash list’命令可以将当前的Git栈信息打印出来,你只需要将找到对应的版本号,例如使用’git stash apply stash@{1}’就可以将你指定版本号为stash@{1}的工作取出来,当你将所有的栈都应用回来的时候,可以使用’git stash clear’来将栈清空
3.4 将一个分支提交到另一个分支上
git push origin develop:caixisheng
4. tag
创建tag
git tag [name]
删除tag
git tag -d [name]
查看tag
git tag
切换tag
git checkout [tagname]
Tag 推送服务器
git push origin v1.0
git push origin --tags 将本地所有tag一次性提交到git服务器
5.查询当前分支状态
git status 显示被修改的文件
git diff 显示具体修改的细节
6.inux git 配置文件修改
1.在~/下, touch创建文件 .git-credentials, 用vim编辑此文件,输入:
https://{username}:{password}@github.com
注意去掉{}
2.在终端下执行 git config --global credential.helper store
3.可以看到~/.gitconfig文件,会多了一项:
[credential]
helper = store
7.版本回退
git reset --hard 返回到当前分支最后一次提交的状态
git reset --hard origin/develop 将当前分支恢复为其他分支的状态
8.远程分支
查看远程分支
git branch -r
拉取远程分支并创建本地分支
first: git fetch
方式1:git checkout -b 本地分支x orign/远程分支x
方式2: git fetch origin 远程分支名x:本地分支名x
9.常见错误 non-fast-forward后的冲突解决:
参考博客:http://blog.csdn.net/chain2012/article/details/7476493
强制推送 -f
git push origin [branch name] -f
参考链接:
https://www.cnblogs.com/blackeyes/p/7636768.html
原文:https://www.cnblogs.com/waken-captain/p/9646571.html