创建仓库
- 克隆远端服务器的仓库:
git clone username@host:/path/to/repository - 初始化本地仓库:
git init
配置身份
git config --global user.name "xx"git config --global user.email "xxx.com"
更新代码
- 更新本地仓库:
git pull,git pull <远程库名> <远程分支名>:<本地分支名> - 合并其他分支到当前分支:
git merge <branch>
- 从本地仓库检出代码:
git checkout . - 从本地仓库检出指定文件夹或文件:
git checkout <path>/<filename>
提交代码
- 添加文件到缓存区:
git add <filename>或git add .
- 检查缓存区是否有文件未提交:
git status - 检查本地文件和仓库文件差别:
git diff <filename> - 将缓存区文件提交到仓库:
git commit -m 'info'
- 将仓库文件提交到远端服务器:
git push origin <branch>
代码分支
-
创建并进入新的分支:
git checkout -b <branch> -
切换到已有分支:
git checkout <branch> -
删除分支:
git branch -d <branch> -
查看已有分支:
git branch -a -
是否与远程建立连接:
git remote -v -
与远程仓库加关联:
git remote add origin <远程库名> -
将本地分支推送到远端分支:
git push origin <本地分支名>:<远程分支名>
回退代码
-
查看历史修改记录:
git log -
查看指定文件的提交历史:
git log -p <filename> -
以列表形式查看文件提交历史:
git blame <filename> -
查看文件内容:
cat <filename> -
回退到上一个版本:
git reset --hard HEAD^ -
获取历史版本号:
git reflog -
回退到指定版本:
git reset --hard 版本号
其他命令
- 查看GIT版本:
git --version - 生成patch:
git format-patch -1 - 打patch:
git apply xx.patch