Git 常用命令
*注:所有[]仅为标注作用,不要在命令内写入
一、克隆
1.克隆(一般默认为 master )
git clone [repositoryPath]
2.克隆指定分支
git clone -b [targetBranchName] [repositoryPath]
二、分支
1.基于当前分支新建分支
git checkout -b [newBranchName] origin/[oldBranchName]
2.本地分支关联远程分支
git push --set-upstream origin [remoteBranchName]
3.查看本地分支
git branch
4.查看全部(本地+远程)分支
git branch -a
5.切换分支
git checkout [targetBranchName]
6.删除本地分支
git branch -d [targetBranchName]
7.删除远程分支
git push origin --delete [targetBranchName]
三、提交-添加到暂存区
1.添加指定文件
git add [file1] [file2]
2.添加指定目录
git add [dir]
3.添加全部文件
git add .
四、提交-本地仓库
1.需先设置用户信息(如去除global,则只对当前仓库有效)
git config --global user.name [userName]
git config --global user.email [email]
2.将暂存区全部文件提交(-m 后的内容是你对这次提交的备注/标识)
git commit -m “注释信息”
3.将暂存区部分文件提交
git commit [file1] [file2] -m “注释信息”
4.将文件添加到暂存区并直接提交到本地仓库(即无需先 git add xxx)
git commit -a -m “注释信息”
五、提交-远程/云端仓库
1.将本地分支代码提交到远程仓库分支
git push [远程主机名] [本地分支名] : [远程分支名]
描述 :
[远程主机名]默认为origin;
如 [本地分支名] 与 [远程分支名] 一致,即可省略 :[远程分支名]
如:
git push [远程主机名] [本地分支名]
2.强制推送本地分支代码到远程仓库分支(不建议使用,如本地与远程有冲突,建议先合并冲突,再push)
git push --force [远程主机名] [本地分支名]
六、拉取
1.将远程分支代码拉取到本地当前分支
git pull [远程主机名] [远程分支名]
2.将将远程分支代码拉取到本地指定分支
git pull [远程主机名] [远程分支名] : [本地分支名]
七、TAG
1.创建tag
git tag -a [tagName] -m "创建tag注释"
2.推送tag到云端
git push origin [tagName]
3.删除本地tag
git tag -d [tagName]
4.删除云端tag
git push origin :refs/tags/[tagName]
七、其他
1.查看当前分支状态,如有未添加到暂存区/本地仓库会显示具体文件
git status
2.将提交过的文件添加到 .ignore
git rm -r --cached [文件全路径(包含文件名称)]
3.查看分支的移动记录,在最下面一行可以看到基于哪个分支创建的该分支
git reflog --date=local | grep [分支名]
示例:git reflog --date=local | grep feature/addLoginParams