6.24
创建git仓库
将工作区中的项目文件使用git进行管理,即创建一个新的本地仓库:git init
从远程git仓库复制项目:git clone url,如:git clone git://github.com/wasd/example.git;克隆项目时如果想定义新的项目名,可以在clone命令后指定新的项目名:git clone git://github.com/wasd/example.git mygit;
git 基本操作
配置用户名:git config --global user.name “your name”
配置用户邮箱:git config --global user.email “youremail@github.com”;
- git init 创建本地仓库初始化
- git add . 把本地代码提交到暂存区
- 提交工作区中指定文件到暂存区:git add <file1> <file2> …;
- 提交工作区中某个文件夹中所有文件到暂存区:git add [dir];
- git rm <file1> <file2>; 从工作区中删除文件
- 重命名文件,并将已改名文件提交到暂存区:git mv [file-original] [file-renamed];
- 查看文件状态 git status
- 对比文件差异 比较工作区中当前文件和暂存区之间的差异,也就是修改之后还没有暂存的内容:git diff;指定文件在工作区和暂存区上差异比较:git diff <file-name>;
分支管理
- 创建分支:git branch ,如git branch testing;
- 从当前所处的分支切换到其他分支:git checkout ,如git checkout testing;
- 新建并切换到新建分支上:git checkout -b ;
- 删除分支:git branch -d ;
- 将当前分支与指定分支进行合并:git merge ;
- 显示本地仓库的所有分支:git branch;
- 查 看各个分支最后一个提交对象的信息:git branch -v;
- 查看哪些分支已经合并到当前分支:git branch --merged;
- 查看当前哪些分支还没有合并到当前分支:git branch --no-merged;
- 把远程分支合并到当前分支:git merge /,如git merge
远程同步
-
下载远程仓库的所有变动git fetch [remote]
-
显示所有远程仓库 git remote -v
-
显示某个远程仓库的信息 git remote show [remote]
-
增加一个新的远程仓库,并命名git remote add [shortname] [url]
-
取回远程仓库的变化,并与本地分支合并 git pull [remote] [branch]
-
上传本地指定分支到远程仓库 git push [remote] [branch]
-
强行推送当前分支到远程仓库,即使有冲突 git push [remote] --force
-
推送所有分支到远程仓库 git push [remote] --all
-
显示当前分支的版本历史 git log
提交代码
- git commit -m “提交信息” 提交到本地
- git remote add origin https://github.com/用户名/仓库名.git 连接远程库
- 使用命令git push -u origin master 上传本地指定分支到远程仓库
- 也可以直接用 git push [url] [分支名] 直接推送到远程仓库