git 常见的操作命令

167 阅读1分钟
  • 基本使用
  1. 初始化git仓库
git init
  1. 添加并上传文件至本地仓库
git add -A
git commit -m "提交记录"
  1. 添加/配置仓库地址
git remote add origin http://dev.github.com:9800/root/front.git

4.提交到远程仓库

git pull origin 分支名
git push origin 分支名
  • 其他常见命令
  1. 克隆
git clone "仓库地址"

2.合并分支

git merge 分支名

3.查看分支

git branch      // 列出当前分支清单

git branch -a   // 查看远程分支和本地分支

4.分支名重命名

// 如果对于分支不是当前分支,可以使用下面代码:
git branch -m 原分支名 新分支名

// 如果是当前,那么可以使用加上新名字
git branch -m 新分支名称

5.切换分支

git checkout 分支名     // 切换到本地(不存在/存在)的分支但远程存在的分支
git checkout -b 分支名  // 创建一个新分支并切换到此分支上

6.回退版本

git log  // 查看提交记录的版本号
git reset --hard 指定回退的版本号
  • 常见问题
  1. 如果在配置仓库出现这行报错如下:
fatal: remote origin already exists.

解决方法:

git remote rm origin    // 先删除
git remote add origin http://dev.github.com:9800/root/front.git  // 后添加
  1. 项目上有一个分支test,使用git branch -a看不到该远程分支,直接使用命令git checkout test报错如下:
error: pathspec ‘origin/test‘ did not match any file(s) known to git.

解决方法:

git fetch           // 取回所有分支的更新
git branch -a       // 可以看到test分支(已经更新分支信息)
git checkout test   // 切换分支