Git简明教程

Git简明教程


1. git工作流和四大域


git四大域工作目录,暂存区,仓库远程仓库

(进行“git stash”操作时,文件可能被保存到四大域外的堆栈中)

工作目录     ———持有实际文件
暂存区Index———缓存区域,git add即可临时保存你的改动
最后是 HEAD———指向git commit的最后一次提交的结果
远程仓库:————本地仓库的代码需要git push后,才能推送到远程版本库。

2. git常用命令


一、设置

git config --global user.name lavor                //配置全局用户名,
git config --global user.email [email protected] //配置全局电子邮箱
git config --global alias.cm commit                //为git命令配置别名
git config --list                                                //显示所有配置信息

二、使用场景

1.新建分支,在分支上修改代码。

创建新仓库

git init                                                                //创建新文件夹,运行以创建新的 git 仓库

检出仓库

git clone /path/to/repository                              //从本地仓库克隆版本

git clone username@host:/path/to/repository   //从远端服务器上的仓库克隆

分支:

git branch                                                        //查看所有分支
git branch branchname  origin/branchname   //以远程库特定的分支为基创建本地分支

git branch -d branchname                              //删除分支
git branch -m [oldbranchname] newbranchname//重名分支
git branch -r                                                    //列出所有被跟踪的远程分支
git branch -r -d branchname                           //删除被跟踪的远程分支
git branch -a                                                    //列出所有本地分支与被跟踪的远程分支


2.提交修改代码

git add files 或 git add .                      //添加工作目录的文件到暂存区
git commit -m "提交消息"                   //提交暂存区的文件,带有提交消息
git status                                            //显示工作树的状态
git pull 或者 git fetch                          //更新本地代码
git push  origin/branchname              //推送到本地分钟所跟踪的远程分支库中。


3.回退或者撤销代码。
  • 还没有commit的代码文件。
  git checkout .                                         //撤销所有已修改的修改,但不包括新增的文件
  git checkout [filename]                          //撤销对指定文件的修改,[filename]为文件名
  • 已经commit但是没有push
  git reset --hard [commit-hashcode]       // [commit-hashcode]是某个 commit 的哈希值,可以用 git log 查看。

                                                                //或者用HEAD~1表示前一次提交,HEAD~2表示前两次提交,以此类推


git reset –mixed:默认方式,不带任何参数的git reset,即时这种方式,它回退到某个版本,只保留源码,回退commit和index信息
git reset –soft:回退到某个版本,只回退了commit的信息,不会恢复到index file一级。如果还要提交,直接commit即可
git reset –hard:彻底回退到某个版本,本地的源码也会变为上一个版本的内容
git reset -soft :只回退了commit的信息。如果还要提交,直接commit即可
git reset -mixed(默认) :取消了commit ,取消了add
git reset -hard :取消了commit ,取消了add,取消源文件修改

  • 已经push了
  git revert  <commit-hashcode>             //用一个新提交来恢复到一个指定的历史提交状态。实现代码回退。

  

4.保存当前的修改文件,重新开发另一功能,然后在恢复


git stash                                  //储藏工作目录与暂存区的状态到堆栈中
git stash pop [stash@{id}]                 //恢复到指定储藏的状态(默认是最近一次储藏的状态),并从堆栈中移除该储藏
git stash apply [stash@{id}]               //恢复到指定储藏的状态(默认是最近一次储藏的状态)
git stash drop [stash@{id}]                //并从堆栈中移除指定储藏(默认是最近一次储藏)
git stash list                             //显示所有储藏信息

5.查看


git log                                                 //查看提交记录
git log  --oneline                                 //查看提交记录,以oneline形式显示,只显示一行
git log  origin/branchname                 //查看远程库里的分支commit日志

git diff                                                //查看工作目录与暂存区的差异
git diff <commit>                               //查看工作目录与指定提交的差异
git diff <commit> <commit>              //查看两次指定提交的差异
git diff branchname                          //查看工作目录与指定分支的差异


参考博文:

http://www.jianshu.com/p/16ad0722e4cc

http://www.runoob.com/manual/git-guide/

猜你喜欢

转载自blog.csdn.net/lingfeng5/article/details/73478096