git常用操作命令

一 推送代码

git push origin master

git push -f origin master # 强制推送

二 拉取代码

git pull origin master

git pull --all # 获取远程所有代码包括tag

git pull origin next:master # 取回origin主机的next分支,与本地的master分支合并

git pull origin next # 远程分支是与当前分支合并

上面一条命令等同于下面两条命令

git fetch origin

git merge origin/next

三 修改远程仓库地址

git remote remove origin # 删除该远程路径

git remote add origin git@jslite.github.com:JSLite/JSLite.git # 添加远程路径

四 撤销远程记录

git reset --hard HEAD~1 # 撤销一条记录

git push -f origin HEAD:master # 同步到远程仓库

五 放弃本地的文件修改

git reset --hard FETCH_HEAD # FETCH_HEAD表示上一次成功git pull之后形成的commit点。然后git pull

git reset --hard FETCH_HEAD 出现错误

解决方法:

复制代码
git checkout -b temp # 新建+切换到temp分支 
git checkout master

六 回退到某一个版本

git reset --hard <hash>

例如 git reset --hard a3hd73r

--hard代表丢弃工作区的修改,让工作区与版本代码一模一样,与之对应,

--soft参数代表保留工作区的修改。

七 回滚到某个commit提交

git revert HEAD~1 # 撤销一条记录 会弹出 commit 编辑

git push # 提交回滚

八 删除分支branch

git push origin :branchName # 删除远程分支

git push origin --delete new # 删除远程分支new

git branch -d branchName # 删除本地分支,强制删除用-D

git branch -d test # 删除本地test分支

git branch -D test # 强制删除本地test分支

git remote prune origin # 远程删除了,本地还能看到远程存在,这条命令删除远程不存在的分支

九 查看

git branch # 列出本地分支

git branch -r # 列出远端分支

git branch -a # 列出所有分支

git branch -v # 查看各个分支最后一个提交对象的信息

git branch --merge # 查看已经合并到当前分支的分支

git branch --no-merge # 查看为合并到当前分支的分支

git remote show origin # 可以查看remote地址,远程分支

十 新建

git branch test # 新建test分支

git branch newBrach 3defc69 # 指定哈希3defc69,新建分支名字为newBrach

git checkout -b newBrach origin/master # 取回远程主机的更新以后,在它的基础上创建一个新的分支

git checkout -b newBrach 3defc69 # 以哈希值3defc69,新建 newBrach 分支,并切换到该分支

十一 分支切换

git checkout - # 快速切换分支上一个分支

git checkout test # 切换到test分支

git checkout -b test # 新建+切换到test分支

git checkout -b test dev # 基于dev新建test分支,并切换