Git 基础知识总结

Git 基础

git 和 svn 对比

1、git 是分布式,svn 是集中式
2、git 把内容按元数据方式存储,svn 按文件进行存储
3、git 分支和 svn 分支不同
4、git 的内容完整性要优于 svn
 

git 原理图

# 提交暂存区到仓库区
git commit -m [message]

# 提交暂存区的指定文件到仓库区
git commit [file1] [file2] ... -m [message]

# 提交工作区自上次commit 之后的变化,直接到仓库区
git commit -a

# 使用一次新的 commit,替代上一次提交
# 如果代码没有任何新变化,则用来改写上一次commit 的提交信息
git commit --amend -m [message]

# 重做上一次 commit ,并包括指定文件的新变化
git commit --amend [file1] [file2] ...
 

分支操作

# 列出所有本地分支
git branch

# 列出所有远程分支
git branch -r

# 列出所有本地分支和远程分支
git branch -a

# 新建一个分支,但依然停留在当前分支
git branch [branch-name]

# 新建一个分支,并切换到该分支
git checkout -b [branch-name]

# 新建一个分支,指向指定commit
git branch [branch] [commit]

# 新建一个分支,与指定的远程分支简历追踪关系
git branch --track [branch] [remote-branch]

# 切换到指定分支,并更新工作区
git chechout [branch-name]

# 切换到上一个分支
git chechout -

# 建立追踪关系,设置当前分支与指定的远程分支之间的关联
git branch --set-upstream-to [remote-branch]

# 合并指定分支到当前分支
git merge [branch]

# 选择一个commit,合并进当前分支
git cherry-pich [commit]

# 删除分支
git branch -d [branch-name]

# 删除远程分支
git push origin --delete [branch-name]
git branch -dr [remote/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

# 新建一个分支,指向某个tag
git checkout -b [branch] [tag]
 

查看信息

# 显示有变更的文件
git status

# 显示当前分支的版本历史
git log

# 显示 commit 历史,以及每次提交commit 发生变更的文件
git log --stat

# 搜索提交历史,根据关键词
git log -S [keyword]

# 显示某个commit 之后的所有变动,每个commit 占据一行
git log [tag] HEAD --pretty=format:%s

# 显示某个commit 之后的所有变动,其“提交说明” 必须符合搜索条件
git log [tag] HEAD --grep feature

# 显示某个文件的版本历史,包括文件改名
git log --follow [file]
git whatchanged [file]

# 显示指定文件相关的每一次diff
git log -p [file]

# 显示过去5次提交
git log -5 --pretty --oneline

# 显示所有提交过的用户,按提交次数排序
git shortlog -sn

# 显示指定文件是什么人在什么时间修改过
git blame [file]

# 显示暂存区和工作区的差异
git diff

# 显示暂存区和上一个commit 的差异
git diff --cached [file]

# 显示工作区与当前分支最新commit之间的差异
git diff [first-branch]...[second-branch]

# 显示某次提交的元数据和内容变化
git show [commit]

# 显示某次提交发生变化的文件
git show --name-only [commit]

# 显示某次提交时,某个文件的内容
git show [commit]:[filename]

# 显示当前分支的最近几次提交
git reflog

远程同步

# 下载远程仓库的所有变动
git fetch [remote]

# 显示所有远程仓库
git remote -v

# 显示某个远程仓库的信息
git remote show [remote]

# 增加一个新的远程仓库,并命名
git remote add [shortname] [url]

# 取回远程仓库的变化,并与本地分支合并
git pull [remote] [branch]

# 上传本地指定分支到远程仓库
git push [remote] [branch]

# 强行推送当前分支到远程仓库,即使有冲突
git push [remote] --force

# 推送所有分支到远程仓库
git push [remote] --all
 

撤销

# 恢复暂存区的指定文件到工作区
git chechout [file]

# 恢复某个commit 的指定文件到暂存区和工作区
git checkout [commit] [file]

# 恢复暂存区的所有我文件到工作区
git checkout .

# 重置暂存区的指定文件,与上一次commit 保持一致,但工作区不变
git reset [file] 

# 重置当前分支的HEAD 为指定commit,同时重置暂存区和工作区,与指定commit 一致
git reset --hard [commit]

# 新建一个commit,用来撤销指定commit
# 后者的所有变化都被前者抵消,并且应用到当前分支
git revert [commit]

# 暂时将未提交的变化移除,稍后再移入
git stach
git stach pop
 

其他

# 生成一个可供发布的压缩包
git archive

# 删除项目的 git 管理
rm .git
 

git 添加指定要忽略的文件

在项目中创建指定文件 .gitignore   将指定要忽略的文件 添加进入这个文件。例如:idea/   *.py[cod]
 

Github 操作说明

登录github注册对应的账号(https://github.com

创建一个项目

New repository

clone项目

进入自己本地的目录,将github 上的项目 通过url 进行本地的拉去
$ git clone https://(url)
 

第一次使用需配置用户名和邮箱

$ git config --global user.name ""
$ git config --global user.email ""
 

测试操作

$ git add test.py
$ git commit -m "init test.py"
$ git push origin master
注:最后需要输入对应的账号和密码
 

ssh配置

Settings --> SSH and GPG keys:可以为项目配置身份密钥,这样不必每次推送都输入账号和密码
ubuntu下生成当前电脑ssh秘钥
ssh-keygen
    1、id_rsa是私钥,自己保管好
    2、id_rsa.pub是公钥,上传至github
将公钥上传至github作为ssh keys
修改项目的配置文件ssh方式,编辑项目里的.git/config文件
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    #url = https://github.com/(url)
    url = git@github.com:(url)
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge =refs/heads/master
 

ssh 密钥生成

1.查看是否已经有了ssh密钥:cd ~/.ssh 如果没有密钥则不会有此文件夹,有则备份删除
    
2.生存密钥: 
$ ssh-keygen -t rsa -C “wanghao@163.cn【个人邮箱】” 按3个回车,密码为空。 
Your identification has been saved in /home/python/.ssh/id_rsa. 
Your public key has been saved in /home/python/.ssh/id_rsa.pub. 
The key fingerprint is: ……………… 最后得到了两个文件:id_rsa和id_rsa.pub

3.添加密钥到ssh:ssh-add 文件名 需要之前输入密码。

4.在github上添加ssh密钥,这要添加的是“id_rsa.pub”里面的公钥。 打开https://github.com/ ,登陆 github账户,然后添加ssh。

5.测试:ssh git@github.com 
The authenticity of host ‘github.com (207.97.227.239)’ can’t be established. RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added ‘github.com,207.97.227.239′ (RSA) to the list of known hosts. ERROR: Hi tekkub! You’ve successfully authenticated, but GitHub does not provide shell access Connection to github.com closed. 
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值