常用的git操作命令
分支操作
获取git项目
git clone [git地址]
切换分支
// 获取远端分支到本地
git checkout -b feature/test origin/feature/test
// 本地分支切换,可以切分支、tag、commitId等
git checkout feature/test
新建分支
// 以当前代码新建分支[feature/test]
git branch feature/test
// 以当前代码新建并直接切换到分支[feature/test]
git checkout -b feature/test
查看分支
// 查看本地分支
git branch
// 查看远端分支
git branch -r
// 查看所有分支
git branch -a
删除分支
// 删除本地分支
git branch -d feature/test
// 删除远端分支
git push origin -d feature/test
git branch -r -d origin/feature/test
更新本地仓库
// 同步远端分支到本地仓库(当前分支已正确配置上游分支)
git fetch
// 同步指定远端仓库分支到本地仓库(当前上游分支是develop,想同步release分支就需要指定)
git fetch origin xxx
// 合并分支
git merge [origin/xxx]
// 同步远端仓库并自动合并(git pull = git fecth + git merge)
git pull
// 同步远端指定分支并触发自动合并
git pull [origin xxx]
// 同步远端分支信息到本地仓库
git remote update --prune
提交代码到分支
// 查看代码的修改状态
git status
// 暂存所有提交的文件
git add .
// 暂存指定文件readme test.java等等
git add readme test.java xxx1
// 撤销add的文件
git reset [文件名]
// 提交已暂存的文件
git commit -m "test message"
// git add 和 git commit -m 可以合并成一条命令
git commit -am "test message"
// 推送到远端关联的分支
git push origin feature/test
分支合并
// 合并本地develop分支(不自动commit)
git merge develop --no-commit
// 合并远端develop分支(不自动commit)
git merge origin develop --no-commit
还原代码到指定版本
- 使用git revert(它是新增记录)
git revert -n <commit-id> // 将代码回退倒commit-id时的版本
git commit -m "revert xx 版本" // 提交
git push origin <branch> // 更新服务器版本,此时服务器会多出一条修改记录
- 使用git reset(它会将HEAD移动到你reset的记录)
git reset --hand <commit-id> // 将本地代码回退到commit-id时的版本
git push -f// 将远端仓库更新,此时HEAD版本变为commit-id;必须使用-f,因为你本地版本比远端的旧
tag操作
- 查看tag
// 查看本地tag
git tag --list
// 查看远端tag
git ls-remote --tags
- 新建tag
git tag v3.0.0
- 删除本地tag
git tag -d v1.0.0
- 删除远端tag
git push origin -d tag v1.0.0
- 将本地tags推送到远端
// 将本地单个tag推送至远端
git push origin v1.0.0
// 将本地tag全部推送至远端
git push origin --tags
- 查看本地分支的git地址
git remote -v
- 更换本地分支的git地址
git remote set-url origin http://xxx.xxx.xxx.git
- 查看tag信息
git show v1.0.0
- checkout tag代码
// 先跟新远端tag到本地
git fetch --all --tags
// checkout tag为新分支代码
git checkout -b version2 v2.0.0
git 文件过大操作处理
一般不建议做此操作,因为我们公司不允许使用具有上传功能的网站,所以我们的apk是放到自己的git服务器上
供测试人员下载,久而久之这会导致git的历史记录过大(每一次记录都是apk文件),下面的介绍是用来删除git的历史记录。
git filter-branch --index-filter 'git rm -r --cached --ignore-unmatch <文件夹名>'
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git fsck --full --unreachable
git repack -A -d
git gc --aggressive --prune=now
git push --force

浙公网安备 33010602011771号