Skip to content

Git常用命令

基本git命令

列出全局配置

bash
git config --list --global

增加用户名

bash
git config --global --add user.name "用户名"

删除用户名

bash
git config --global --unset user.name "用户名"

查询用户名

bash
git config --get user.name

修改用户

bash
git config --global user.name "用户名"

将文件夹变为git仓库

bash
git init

添加到暂存区

bash
git add "文件名"

添加到版本库

bash
git commit -m"版本号"

推送

bash
git push

另一些git命令

从仓库下载

bash
git clone "链接地址"

打印日志

bash
git log
git log --pretty=oneline #只打印每次提交的版本号

回到之前的版本

bash
git reset --hard "版本号"

查看历史命令

bash
git reflog

打印最近两次版本信息

bash
git log -n2 --oneline

仓库连接

bash
git remote add origin xxx
git push -u origin master
git remote -v
//取消远程关联仓库
git remote remove origin

ssh

生成ssh密钥

bash
ssh-keygen -t rsa -C "邮箱"

将~/.ssh/id_rsa.pub文件内容粘贴到github的ssh key里

分支管理

创建分支

bash
git branch <branch-name>

切换分支

bash
git checkout <branch-name>

创建+切换分支

bash
git checkout -b <branch-name>

查看分支

bash
git branch

创建分支和远程分支连接

bash
git checkout -b <branch-name> origin/
<branch-name>

合并分支

bash
git merge <branch-name>
git merge --no-ff -m"xx" <branch-name>

查看分支图

bash
git log --graph --pretty=oneline --abbrev-commit

将修改内容添加到工作区的存储区

bash
git stash

查看存储区的内容

bash
git stash list

恢复存储区的内容

bash
git stash pop

删除分支

bash
git branch -d <branch name>
git branch -D <branch name>

推送分支

bash
git push orgin <branch name>

推送

您还可以按照以下说明从计算机中上传现有文件。

Git 全局设置

shell
git config --global user.name "nard chen"
git config --global user.email "nard.work@outlook.com"

创建一个新仓库

shell
git clone git@gitlab.com:nard.chen/my-documents.git
cd my-documents
git switch --create main
touch README.md
git add README.md
git commit -m "add README"
git push --set-upstream origin main

推送现有文件夹

cd existing_folder
git init --initial-branch=main
git remote add origin git@gitlab.com:nard.chen/my-documents.git
git add .
git commit -m "Initial commit"
git push --set-upstream origin main

推送现有的 Git 仓库

cd existing_repo
git remote rename origin old-origin
git remote add origin git@gitlab.com:nard.chen/my-documents.git
git push --set-upstream origin --all
git push --set-upstream origin --tags

未经许可禁止任何形式的转载