git 常用命令

387 阅读1分钟

git配置 git config user.name 'xxx' // 用户名
git config user.email 'xxx' // 邮箱

分支常用命令 以下的 name 指分支的名称

创建分支

git branch name

查看分支

git branch     // 查看本地所有分支
git branch -r     // 查看远程仓库所有分支

查看变更的文件

git status // 显示有变更的文件

切换分支

git checkout name

合并分支

git merge name

删除本地分支

git branch -d name

切换并创建分支

git checkout -b name

git pull 和 git fetch 的区别

git fetch 只是将远程仓库的变化下载下来,并没有和本地分支合并。
git pull 会将远程仓库的变化下载下来,并和当前分支合并。

《常用 git 命令清单》

注意!!! 每次在切换分支前 提交一下当前分支

提交分支 git add .    // 将文件加入到暂存区
git commit -m '注释信息'    // 提交到本地仓库
git push orgin 分支名    // 推送到远程仓库

注意!: 如果我们是在本地写的代码就要先连接远程仓库在进行操作 git remote add origin 仓库地址,同时我们也可以通过 git remote -v 查看远程仓库地址。

若远程地址发生了更改,更改远程地址并提交代码到新的远程地址

git remote  //查看所有远程仓库
git remote rm origin  //删除老的远程仓库
git remote add origin 新的git地址  //添加新的远程仓库地址
git add .     //将代码先存入暂存区没有提交到本地仓库
git commit -m "   //叙述的内容" 将代码提交到本地仓库
git push -u origin 分支名   //第一次提交加-u