ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Git 基础知识总结

2022-08-28 09:30:55  阅读:211  来源: 互联网

标签:总结 git -- tag 基础知识 Git branch commit 分支


Git 基础

git 和 svn 对比
1、git 是分布式,svn 是集中式
2、git 把内容按元数据方式存储,svn 按文件进行存储
3、git 分支和 svn 分支不同
4、git 的内容完整性要优于 svn
git 的安装( https://git-scm.com/download/linux)
apt-get install git
git 原理图

Git 常用操作命令

官方文档( https://git-scm.com/book/zh/v2)
创建代码仓库
# 在当前目录新建一个Git 代码库
git init

# 新建一个目录,将其初始化为Git 代码库
git init [project-name]

# 下载一个项目和它的整个代码历史
git clone [url]
配置git 的设置文件 .gitconfig (注:在用户主目录下,为全局配置;在项目目录下,为项目配置)
# 显示当前的Git 配置
git config --list

# 编辑Git 配置文件
git config -e [--global]

# 设置提交代码时的用户信息
git config [--global] user.name "wanghao"
git config [--global] user.email "wanghao@itcast.cn"
添加/删除 文件
# 添加指定文件到暂存区
git add [file1] [file2] ...

# 添加指定目录到暂存区,包括子目录
git add [dir]

# 添加当前目录的所有文件到暂存区
git add .

# 删除工作区文件,并且将这次删除放入暂存区
git rm [file1] [file2] ...

# 改名文件,并且将这个改名放入暂存区
git mv [file-original] [file-renamed]
代码提交
# 提交暂存区到仓库区
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. 

标签:总结,git,--,tag,基础知识,Git,branch,commit,分支
来源: https://www.cnblogs.com/qingtianyu2015/p/16632260.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有