Skip to main content

git

免费服务

  1. gitbub

  2. gitlab

  3. sf.net

自己部署

  1. linux

  2. gitlab

  3. OneDev - 轻量级的GitLab替代品 https://github.com/theonedev/onedev

  4. gogs - 轻量级的GitLab替代品 https://gogs.io/docs/installation/install_from_binary

图形化工具

  1. sourcetree
  2. Tortoisegit
  3. vscode pycharm 内置

图解

img

Workspace: 工作区

Index/Stage: 暂存区

Repository: 仓库区(本地仓库)

Remote: 远程仓库

命令

初始化
git init
git init test

下载到本地
git clone url

把当前目录的所有 待定的文件添加到git中管理起来
git add .
git add [file1] [file2]
git add [dir]
停止追踪指定文件,但该文件会保留在工作区 ex:配置文件 包含密码
git rm --cached [file]

把暂存的文件提交到本地git的一个版本记录中
git commit -m "提交日志信息(一般填写了啥)"
把当前分支推送到远程origin仓库
git push
git push -u origin master
把远程信息拉到本地
git pull



检测本地仓库中是否有新文件
git status
查看提交日志
git log


#创建并且新分支
git checkout -b branch_name
切换到指定分支,并更新工作区
git checkout [branch-name]
#列出所有本地分支和远程分支
git branch -a
列出所有远程分支
git branch -r
列出所有本地分支
git branch
# 删除分支
git branch -d localBranchName
删除远程分支
git push origin --delete [branch-name]
将现有分支与指定远程分支建立追踪关系
git branch --track [branch] [remote-branch]
合并指定分支到当前分支
git merge [branch]


列出所有tag
git tag
新建一个tag在当前commit
git tag [tag]
新建一个tag在指定commit
git tag [tag] [commit]
删除本地tag
git tag -d [tag]
删除远程tag
git push origin :refs/tags/[tagName]
查看tag信息
git show [tag]
提交指定tag
git push [remote] [tag]
提交所有tag
git push [remote] --tags


环境相关
git config --global user.name "用户名"
git config --global user.email "用户邮箱"

//去除ssl校验,从而使用https协议
git config --global http.sslVerify false

//本地保存账号密码,避免每次https下载都需要输入账号密码
git config --global credential.helper store

//不使用自动回车符转换
git config --global core.autocrlf false

//linux下设置日志模板
git config --global commit.template ~/git/commit_message.txt


#回退操作 已经commit HEAD^的意思是上一个版本,也可以写成HEAD~1 ;如果你进行了2次commit,想都撤回,可以使用HEAD~2
git reset --soft HEAD^

#回退操作 没有commit
git reset HEAD


将现有仓库与远程仓库建立联系
git remote add origin [remote address]
下载远程仓库的所有变动
git fetch [remote]
显示所有远程仓库
git remote -v
取回远程仓库的变化,并与本地分支合并
git pull [remote] [branch]
上传指定分支到远程仓库
git push [remote] [branch]
推送所有分支到远程仓库
git push [remote] --all


暂存
git stash
查看暂存记录
git stash list
恢复暂存并删除这个记录
git stash pop stash@{index}
恢复暂存并保留这个记录
git stash apply stash@{index}
删除某个暂存记录
git stash drop stash@{index}
删除全部暂存记录
git stash clear


把之前的提交应用于当前分支
git cherry-pick bd9fcd1

清除未暂存的文件 忽略修改
git restore .