您的位置:首页 > 其它

Git 基础命令

2015-03-13 17:19 92 查看
创建
本地修改
提交历史
分支与标签
更新与发布
合并与重置
撤销
参考


创建

复制一个已创建的仓库:
$ git clone ssh://user@domain.com/repo.git


创建一个新的本地仓库:
$ git init



本地修改

显示工作路径下已修改的文件:
$ git status


显示与上次提交版本文件的不同:
$ git diff


把当前所有修改添加到下次提交中:
$ git add .


把对某个文件的修改添加到下次提交中:
$ git add -p <file>


提交本地的所有修改:
$ git commit-a


提交之前已标记的变化:
$ git commit


附加消息提交:
$ git commit-m 'message here' 


修改上次提交

Don't amend published commits!
$ git commit--amend 



提交历史

从最新提交开始,显示所有的提交记录(显示hash, 作者信息,提交的标题和时间):
$ git log


显示所有提交(仅显示提交的hash和message):
$ git log--oneline 


显示某个用户的所有提交:
$ git log--author="username" 


显示某个文件的所有修改:
$ git log-p <file> 


谁,在什么时间,修改了文件的什么内容:
$ git blame <file>



分支与标签

列出所有的分支:
$ git branch


切换分支:
$ git checkout <branch>


基于当前分支创建新分支:
$ git branch<new-branch> 


基于远程分支创建新的可追溯的分支:
$ git branch--track <new-branch> <remote-branch> 


删除本地分支:
$ git branch-d <branch> 


给当前版本打标签:
$ git tag <tag-name>



更新与发布

列出对当前远程端的操作:
$ git remote -v


显示远程端的信息:
$ git remote show <remote>


添加新的远程端:
$ git remote add <remote> <url>


下载远程端版本,但不合并到HEAD中:
$ git fetch <remote>


下载远程端版本,并自动与HEAD版本合并:
$ git remote pull <remote> <url>


将远程端版本合并到本地版本中:
$ git pull origin master


将本地版本发布到远程端:
$ git push remote <remote> <branch>


删除远程端分支:
$ git push <remote> :<branch> (since Git v1.5.0) or git push <remote> --delete <branch> (since Git v1.7.0)


发布标签:
$ git push --tags



合并与重置

将分支合并到当前HEAD中:
$ git merge <branch>


将当前HEAD版本重置到分支中:

Don't rebase published commit!
$ git rebase <branch>


退出重置:
$ git rebase --abort


解决冲突后继续重置:
$ git rebase --continue


使用配置好的merge tool 解决冲突:
$ git mergetool


在编辑器中手动解决冲突后,标记文件为
已解决冲突

$ git add <resolved-file>

$ git rm <resolved-file>



撤销

放弃工作目录下的所有修改:
$ git reset --hard HEAD


移除缓存区的所有文件(i.e. 撤销上次
git add
):
$ git reset HEAD


放弃某个文件的所有本地修改:
$ git checkout HEAD <file>


重置一个提交(通过创建一个截然不同的新提交)
$ git revert <commit>


将HEAD重置到上一次提交的版本,并放弃之后的所有修改:
$ git reset --hard <commit>


将HEAD重置到上一次提交的版本,并将之后的修改标记为未添加到缓存区的修改:
$ git reset <commit>


将HEAD重置到上一次提交的版本,并保留未提交的本地修改:
$ git reset --keep <commit>


参考

更深入的学习可https://www.atlassian.com/git/tutorials中的Getting Started和Collaborating

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


通过命令大全以及平时总结归纳出的常用命令


1)Git global setup:

git config --global user.name "f_zhou"

git config --global user.email "f_zhou@Ctrip.com"


2)常用命令

git add *.txt // 添加需要提交的文件到暂存区

git commit -m “test” *.txt // 提交暂存区的文件

git commit -a -m 'test' // 直接提交到仓库

git rm readme // 从仓库中删除文件

git rm --cached readme.txt // 从仓库中删除文件, 并保留开发目录下的文件

git rm \*.log // 递归删除当前目录下面所有*.log的文件

git rm *.log // 仅删除当前目录下面的*.log文件

git checkout path/to/file // 只是工作目录中文件被删除,直接checkout该文件即可

git checkout pre_commit_id path/to/file //删除了文件并已commit,可分根据历史版本id恢复

git mv file_from file_to // 改名

gitk // 图形化工具

git commit --amend // 修改最近提交的评述

git remote -v // 显示远程仓库列表

git fetch [remote-name] // fetch 命令只是将远端的数据拉到本地仓库,并不自动合并到当前工作分支

git pull // 从远端仓库抓取数据,并合并到工作目录中当前分支

git push origin master // 推送数据到远程仓库

git diff // 比较工作目录中当前文件和暂存区域快照之间的差异

git diff --cached // 比较已经暂存起来的文件和上次提交时的差异

git diff commit_id_1:path/to/file commit_id_2:path/to/file //
比较两次提交历史中指定文件的差异

git branch -av // 查看本地仓库的分支信息

git fetch origin // 获取远程origin所有仓库信息

git merge origin/dev // merge

git push origin dev // 上传到origin/dev远程仓库分支

git checkout dev // 切换本地分支

git checkout master // 切换主干

git branch -d dev // 删除分支

git branch -D dev // 删除分支(强制执行)

git remote -v // 显示远程仓库信息

git branch dev // 创建分支

git branch --merged // 查看哪些分支已被并入当前分支

git branch --no-merged // 查看尚未合并的分支

git ls-files -d | xargs -i git checkout {} //同时恢复多个被删除的文件


当本地分支被删除想恢复的方法。

1. 通过git log -g命令来找到我们需要恢复的信息对应的commitid,可以通过提交的时间和日期来辨别

2. 通过git branch recover_branch commitid 来建立一个新的分支
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: