Git 基础命令操作指引
2016-04-26 13:58
459 查看
1.Git 初始化
Create a new repositorygit clone git@*******/test.git cd test touch README.md git add README.md git commit -m "add README" git push -u origin master
Existing folder or Git repository
cd existing_folder git init git remote add origin git@@*******/test.git git add . git commit
2.Git Config
config 配置文件位置All User : /etc/gitconfig Current User : ~/.gitconfig
config 查看
git config --list
config 设置
git config --global user.name "xxx" git config --global user.email "xxxx@163.com"
3.Git 基础命令
git status查看git仓库文件状态, git文件状态如下所示。
unstaged - git仓库中没有此文件的相关记录 modified - git仓库中有这个文件的记录,且此文件当前有改动 staged - 变更的文件被暂存,没有提交到仓库 (git add命令之后) commited - 文件被提交到本地git仓库 (git commit之后)
git log
查看历史commit记录
git add
git add file/fileDir #modified或unstaged的文件转换为staged状态 git add -A/--all #modified或unstaged的所有文件转换为staged状态
git rm
git rm test.java #从git仓库删除test.java文件,同时删除本地文件 git rm --cache test.java #从git仓库删除test.java文件,不删除本地文件
git commit
git commit -m "xxx comment" #staged状态转换为commited状态
git diff
git diff file-path #查看自己本次要提交的文件有哪些改动
git pull
git pull [remote-name branch-name] #获取远程服务器数据,并同时与本地数据合并(可能会出现冲突)
git fetch
git fetch [remote-name] #(获取仓库的所有更新,但是不自动合并当前分支) git fetch -p #(获取仓库的所有更新,且自动删除不需要的分支,更新最新的分支下来)
git push
git push [remote-name branch-name] #上传数据到远程分支,若出现冲突,则上传失败。 git push --force [remote-name branch-name] #强制上传数据到远程分支**,(覆盖远程数据,慎用)**
git stash
可用来暂存当前正在进行的工作,比commit简单,而且可以跨分支合并
git stash #暂存更改的内容 git stash save "comment" #暂存更改的内容,带注释 git stash list #显示所有暂存的内容 git show stash@{0} #显示指定stash的更改内容,类似于git diff git stash apply stash@{1} #应用指定编号的stash, 但不从stash-list中清除 git stash pop #应用栈顶的stash,并从stash-list清除该记录 git stash clear #清除stash-list git stash --help #获取更多的信息
3.Git 分支
git branchgit branch #查看本地分支列表 git branch -r #查看远程分支列表 git branch -a #查看所有分支 git branch -d branch-name #删除指定分支 git branch -D branch-name #强制删除指定分支
git checkout
git checkout branch-name #切换到指定分支 (切换前记得先commit或者stash) git checkout -b new-branch-name #基于当前分支创建一个新分支 git checkout -b branch-name origin/branch-name #获取远程分支到本地 git checkout file-name #获取远程最新文件 git checkout -- . #放弃本地所有的modify文件改动
add remote branch
1.git checkout branch-name #切换到要上传的分支 2.git remote add origin branch-name #添加远程分支 3.git push oring branch-name #推送数据到远程分支 (自动创建分支,step2可省去)
delete remote branch
git push origin :remote-branch #删除指定远程分支
4.Git 分支合并
git merge1. git merge branch-name #合并指定分支内容到当前分支 2. git add -A #手动解决冲突后,重新暂存 2. git commit -m "xxx 解决冲突" #再次提交
git cherry-pick
1. git cherry-pick commit-id #合并指定commit到当前分支 2. git add -A #手动解决冲突后, 重新暂存 3. git cherry-pick -- continue #继续刚才的pick操作,这一步容易忘记 4. git cherry-pick -- abort #放弃cherry-pick操作
5.Git 修改与撤销
git resetgit reset HEAD file #将文件从staged转换为unstaged git reset HEAD . #将所有文件从staged转换为unstaged git reset --mixed commit-id #默认方式,回退到指定个版本,只保留源码,回退commit和index信息 git reset --soft commit-id #回退到某个版本,只回退了commit的信息, #不会恢复到index file级。如果还要提交,直接commit即可 git reset --hard commit-id #彻底回退到某个版本,本地的源码也会变为上一个版本的内容
git reflog
显示所有过去commit历史,可用来挽救–hard的误操作
git rebase
(1) git rebase -i commit-id #在vi编辑器中,显示commit-id到最新的所有commit记录 (2) git rebase -i HEAD~10 #在vi编辑器中,显示最新10条commit记录 (3) 在vi编辑器中, 修改commit内容, wq保存 (4) git add -A #解决冲突后,暂存文件 (5) git rebase --continue #完成未完成rebase操作 (6) git rebase --abort #完成未完成rebase操作
在vim编辑器中,常见几种操作方式,显示如下,
# 修改前 pick 8e5da5f test-comment-1 pick 627a433 test-comment-2 pick 627a433 test-comment-3 pick dcc8310 test-comment-4 # commit-id更改 pick 8e5da5f test-comment-1 pick 627a433 test-comment-2 r 627a433 修改后的内容 pick dcc8310 test-comment-4 # commit-id换序 pick 8e5da5f test-comment-1 pick 627a433 test-comment-3 pick 627a433 test-comment-2 pick dcc8310 test-comment-4 # commit-id删除 pick 8e5da5f test-comment-1 pick 627a433 test-comment-2 pick dcc8310 test-comment-4 # commit-id 3 与 4合并 pick 8e5da5f test-comment-1 pick 627a433 test-comment-2 pick 627a433 test-comment-3 s dcc8310 test-comment-4
相关文章推荐
- Spring注解入门
- linux下安装mysql5.5.16
- System.BadImageFormatException: 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B) 在 YMS.FingerLogin.Form1.mxGetImage(Int32 iDeviceIndex, Byte[] imageBuf, Int64 nTimeOut, Int32 iFlagLeave)
- ajax缓存与浏览器差异
- 关于extjs页面打不开问题
- Node.Js 环境配置
- MySQL函数
- iptables详细解释
- 网络流量监控
- 电磁干扰滤波器的设计
- angular-schema-form 自动表单生成
- Win8.1其他浏览器能上网,IE不能上网解决办法
- Oracle-BPM(十)
- 你看他牛,因为他比你努力
- HTML 5.1来了 9月份正式发布 更新内容预览
- position详解
- 趣味分数-辗转相除 (欧几里德算法) 递归算法 求最大公约数-java
- (OK) WPS会一直卡住,无法操作
- Service的工作过程
- 向数组中插入元素