likes
comments
collection
share

Git常用命令

作者站长头像
站长
· 阅读数 8

1.对git进行初始化配置

// 配置git的全局配置
git config --global user.name 用户名
git config --global user.name zahngsan
git config --global user.password 密码
git config --global user.password 123
git config --global user.password 邮箱
git config --global user.email "666@qq.com"

2.本地仓库已建立的情况下与远程仓库建立链接

// 本地代码已完成需要与远程仓库建立链接
git remote add origin <url>
// 更换远程仓库的链接
git remote set-url origin <new url>
// 查看远程仓库的链接
git remote -v
// 删除远程仓库链接
git remote remove <url>

3.本地无仓库获取远程仓库代码

// clone指定分支代码
git clone origin -b branch-name
// 不同分支拉取代码
git pull origin remote-branch-name --allow-unrelated-histories

4.操作本地分支或远程分支

// 删除本地分支
git branch -d dev 或强制删除 git branch -D dev
// 删除远程分支
git push origin --delete remote-branch-name
// 推送代码到远程分支,但远程分支不存在时
git push origin local-branch-name:remote-branch-name
// 切换分支
git checkout branch-name
// 切换并新建分支
git checkout -b branch-name
// 查看远程分支
git branch -r
// 查看所有分支
git branch -a

5.推送代码到远程仓库

// 推送到远程指定分支
git push origin branch-name
// git强制推送到远程仓库
git push -f origin master或者git push -f origin master:develop或者git push origin master --force

6.回滚代码

// 先git log获取到需要回滚的commitid
// 回滚到指定commit
git reset --hard commitid

7.合并代码

// 在当前分支合并另一分支的代码
git merge branchname

8.忘记拉去最新代码,直接工作导致的代码冲突

// 先暂存更改代码
git stash
// 合并完代码后
git stash pop
// 解决冲突