git常用命令总结大全

1、创建仓库和配置仓库

# 初始化仓库
git init
git add .
git commit -m "init commit"
git remote add origin <https://...>
git push -u origin master

# 查看git用户名和邮箱
git config user.name
git config user.email

# 设置全局用户名和邮箱
git config --global user.name <username>
git config --global user.email <useremail>

2、设置远程代码库

# 查看远程仓库地址
git remote -v

# 添加远程仓库(git远程库的默认名称是origin,也可指定其他名字)
git remote add <remote_name> <url>

# 删除远程仓库
git remote rm <remote_name>

# 重命名远程仓库
git remote rename <old_name> <new_name>

# 添加两个远程库,一个是github,一个是gitee
git remote add github <github地址>
git remote add gitee <gitee地址>

# 分别推送两个远程库
git push github master
git push gitee master

3、提交-推送-拉取

# 添加到暂存区,和提交到版本库
git add . //添加了所有文件到暂存区
git commit -m <msg_info>

# 远程推送
# 注意:git远程库的默认名称是origin,也可取其他名字
git push <remote_name> <branch_name>

# 推送举例
git push origin master
git push origin dev

# 设置默认推送的远程和分支。
git push -u <remote_name> <branch_name>

# 设置默认远程和分支推送,举例。以后就可以直接使用git push命令,
# 而不用输入远程和分支了。
git push -u origin master
git push

# 拉取远程代码
git pull <remote_name> <branch_name>

4、查看历史提交

git log
git log --oneline

5、创建-切换-删除-合并分支

# 查看所有分支
git branch

# 创建分支
git checkout -b <branch_name>

# 切换分支
git checkout <branch_name>

# 删除分支
git branch -d <branch_name>

# 合并分支,指定分支合并到当前分支
git merge <other_branch>

6、标签

# 注意:标签只是一个标记,不是分支,而是分支上的某一个提交

# 查看所有标签(所有分支的所有标签都会显示)
git tag

# 创建标签
git tag -a <tag_name> -m <tag_message>

# 创建标签举例
git tag -a v3.1.3 -m "完成唐僧皮肤上线"

# 删除标签
git tag -d <tag_name>

# 切换到指定标签
git checkout <tag_name>

# 推送所有标签
git push <remote_name> --tags

7、查看当前版本库状态

git status

8、版本回退(2种方法)

# 回退到某一个提交,并且删除这个提交之后的所有内容
git reset --hard <commit_id>

# 强制将本地推送到远程,将远程的与本地同步。这样多人合作时,别人需要重新拉代码
git push -f origin master

# 方法二,没有测试。使用git revert
后续补充。。

9、git忽略文件

后续补充。。

10、克隆代码库

# 克隆代码库,默认所有分支标签
git clone <url>

# 克隆指定分支
git clone -b <branch_name> <url>

# 克隆指定分支dev举例
git clone -b dev https://gitee.com/LadissonGittee/git_learn_notes.git

11、删除远程

# 删除远程
git remote rm <remote_name>
# 添加远程链接
git remote add <remote_name> <url>

12、克隆指定分支

# clone指定分支
git clone -b <branch_name> <url>
# 举例
git clone -b dev https://github.com/LadissonLai/WebIDE.git

13、克隆全部仓库并切换分支

# git clone 默认情况下会将所有分支clone下来,但是只显示master分支
git clone <url>
# 查看本地和远程所有分支
git branch -a
# 切换到远程dev分支(本质上是创建一个空本地分支,然后与远程分支同步)
git checkout -b <branch_name> <remote_name>/<branch_name>
# 举例:切换到远程dev分支
git checkout -b dev origin/dev

猜你喜欢

转载自blog.csdn.net/baidu_39140291/article/details/126073546