git 经常使用命令整理

根据本身在项目中使用比较多的,总结了如下比较经常使用的git命令:html

 

#关系图
远程仓库(Remote) - 本地仓库(Repository) - 暂存区(Index) - 本地工做空间(Workspace)jquery

-------------------------------------------------
#git我的邮箱昵称配置
git config --global user.email "xxx@example.com"
git config --global user.name "xxx"git

若是用了 --global 选项,那么更改的配置文件就是位于你用户主目录下的那个,之后你全部的项目都会默认使用这里配置的用户信息。若是要在某个特定的项目中使用其余名字或者电邮,只要去掉 --global选项从新配置便可,新的设定保存在当前项目的 .git/config 文件里。github

 

#查看已配置信息安全

git config --listapp

#直接查阅某个环境变量的设定ssh

git config user.nameide

 

#生成秘钥
ssh-keygen -t rsafetch

 

#git clone时出现
Permissions 0670 for '/home/xxx/.ssh/id_rsa' are too open.
直接
chmod 400 ~/.ssh/id_rsa
便可ui

#秘钥存放地址
cd ~/.ssh

#仓库初始化(删除仓库的话把目录下的 .git 目录删除便可)
git init

tips: ls -al 会看到有个.git文件夹

#仓库初始化(裸仓)
git init --bare
-------------------------------------------------

 

-------------------------------------------------
#把文件加到暂存区(stage/index)
git add test.txt
或(.表明当前目录下的全部)
git add .

#提交(暂存区的全部内容提交到本地仓库)
git commit -m "commit mark"

#提交(-a 会将修改或者删除的文件自动加到暂存区,可是新增文件不会;若是没有新增文件的话就至关于add & commit)
git commit -a -m "commit mark"

#查看仓库当前的状态
git status
-------------------------------------------------

 

-------------------------------------------------
#克隆(-o 参数能够命名远程主机名,默认为origin)
git clone https://github.com/jquery/jquery.git
git clone -o qewr https://github.com/jquery/jquery.git

#列出全部远程仓库
git remote -v

#查看远程仓库详细信息
git remote show <主机名>

#将一个库加到本身主机上 命名为 qwer
git remote add <主机名> <网址>
git remote add qwer git@github.com:ddmmddmm/wuziqi.git

#删除远程库配置
git remote rm <主机名>
git remote rm qwer

#更改远程仓库名称
git remote rename <原主机名> <新主机名>
git remote rename qwer getAsdf
-------------------------------------------------

 

-------------------------------------------------
#下载远程仓库的全部变更
git fetch [remote]

#取回远程仓库的变化,并与本地分支合并
git fetch origin master
git pull [remote] [branch]

#将本地分支推送到远程仓库
git push origin dev:dev
意思是“上传我本地的 dev 分支到远程仓库中去,仍旧称它为 dev 分支”
或者 git push origin dev

#将本地分支推送到远程仓库并创建追踪关系
git push -u origin dev:dev
-------------------------------------------------

 

-------------------------------------------------
#恢复暂存区的指定文件到工做区
git checkout [file]

#重置暂存区与工做区,与上一次commit保持一致
git reset --hard

#回退到上个commit
git reset --hard HEAD^

#回退到上上个commit(在Git中,用HEAD表示当前版本,上一个版本就是HEAD^,上上一个版本就是HEAD^^,固然往上100个版本写100个^比较容易数不过来,因此写成HEAD~100)
git reset --hard HEAD~2

#重置当前分支的HEAD为指定commit,同时重置暂存区和工做区,与指定commit一致
$ git reset --hard [commit]

#撤销某个commit的修改,并自动提交一个commit
(revert 撤销一个提交的同时会建立一个新的提交。这是一个安全的方法,由于它不会重写提交历史,而reset会直接移动分支指针,revert只针对一个)
git revert [commitId]

#查看某个文件修改内容
git diff test.txt

#显示具体的某次的改动的修改
git show [commitId]

#查看提交记录(--pretty=oneline 参数能够单行显示)
git log

#查看操做记录(若是回退版本以后,又想恢复到当前版本,能够经过此命令查到commit_id,从而恢复)
git reflog

#删除文件(记得commit)
git rm xxx

#删除远端的文件夹,但不改动到本地目录
例如删除远端的.idea文件夹,注意此句执行以后须要commit 和 push
(--cached: Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.)
git rm -r --cached \.idea

-------------------------------------------------

 

-------------------------------------------------
#查看本地分支
git branch

#列出全部远程分支
git branch -r

#列出全部本地分支和远程分支
git branch -a

#建立分支
git branch dev

#切换分支
git checkout dev

#建立+切换分支
git checkout -b dev

#合并dev到当前分支
git merge dev

#删除分支
git branch -d dev

#查看分支的追踪关系
git branch -vv

#本地分支与远程分支创建关联(git pull,git push时能够不用指定,另外一个更为简洁的方式是初次push时,加入-u参数)

git branch --set-upstream-to=origin/dev_xxx dev_xxx

-------------------------------------------------

 

-------------------------------------------------
#列出全部tag
git tag

#新建一个一个叫"myTag"的tag在当前commit
git tag myTag

#新建一个tag在指定commit
git tag myTag [commit]

#删除本地指定tag
git tag -d myTag

#查看tag信息
git show myTag

#提交指定tag
git push origin myTag

#提交全部tag
git push origin --tags
-------------------------------------------------

 

-------------------------------------------------
#将当前的修改储藏起来
git stash

#查看储藏堆栈
git stash list

#从储藏中恢复最近一个,但不删除
git stash apply

#从储藏中恢复指定的stash,但不删除(stash@{2}在list中能够看到)
git stash apply stash@{2}

#从储藏中恢复最近一个,并删除(stash@{2}在list中能够看到)
git stash pop

#从储藏中恢复指定的stash,并删除(stash@{2}在list中能够看到)
git stash pop stash@{2}

#从储藏中删除
git stash drop stash@{2}
-------------------------------------------------

 

参考:

#git的使用
https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

#git - 简明指南
http://rogerdudler.github.io/git-guide/index.zh.html

#经常使用 Git 命令清单http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html

相关文章
相关标签/搜索