一、git切换源(仓库)地址命令
1、查看源地址:git remote -v
2、切换源地址: git remote set-url origin 仓库地址url
git remote set-url origin http://git.xxx.site/dev-xxx/apartment/suzhou/front/monitor.git
3、删除远端别名对应的源:git remote rm <name>
二、git代码操作命令整理
1、git仓库建立及初始化推送仓库
本地初始化:git init
添加本地文件:
git add 文件名(包括路径) 添加单个文件,
git add -A 提交所有变化, 【包括git add.和git add -u功能】,
git add -u 提交被修改(modified)和被删除(deleted)文件,不包括新文件(new),
git add . 提交新文件(new)和被修改(modified)文件,不包括被删除(deleted)文件,选择自己需要的命令即可;
提交命令:git commit -m "这里写本次提交的注释"
提交远程仓库:
git remote add origin URL(远程仓库位置)
git push -u origin master 代码更新至远程仓库
2、分支相关操作
创建分支:
git branch <new-branch> # 创建新分支
git checkout <new-branch> # 切换到新分支
git checkout -b <new-branch> # 创建并切换到新分支
查看分支状态:git branch (*代表当前所在分支)
删除分支:
本地:git branch -d <branch_name>
git branch -D <branch_name> (该分支还没有合并到当前所在的分支上,强制删除时命令)
远程:git push origin <branch-name>: <branch-name> --delete (这个命令会将本地分支推送到远程仓库,并删除远程分支)
重命名分支:git branch -m <old-branch-name> <new-branch-name>
git push origin :<old-branch-name> <new-branch-name>
分支合并:
git checkout target_branch(切换至目标分支)
git merge source_branch(合并源分支到目标分支)
手动修改冲突,查看状态:git status
git commit -m "这里写本次提交的注释"(提交修改)
git push origin target_branch(提交修改)
分支拉取命令:
git fetch <remote> (Fetch仓库中所有分支。同时也会下载指定远端的所有commits和文件)
git fetch <remote> <branch>(获取指定分支更新内容)
git fetch --all (获取远端所有分支)
git pull origin master:brantest (将远程分支与本地分支合并)
git pull origin new-branch(拉取指定分支)
代码回滚相关命令:
git log --oneline # 查看最近的提交记录并复制需要回滚的 commit ID
git revert <commit_id> # 将指定的提交记录还原到当前分支上,只回滚对应的commit id提交记录。
git log --oneline # 查看最近的提交记录并复制需要回滚的 commit ID
git reset <commit_id> # 将 HEAD 移动到指定的提交记录上,会丢失commit id之后的提交内容。