git的使用

117 阅读2分钟

前言

Git(读音为/gɪt/)是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。

1. 安装

  • linux下安装
    $ apt install git
    
  • windows直接下载安装即可!

2. 给git新建一个文件夹

3. 初始化本地文件夹

cd 'new folder'
git init

4. 添加远程仓库, 并pull到本地

# Add a remote repository, from the remote repository to pull the repository files to the local
git remote add origin https://gitee.com/username/reponame 
git pull origin master

5. 把文件放到git文件夹

Place the files to be uploaded under the new folder

6. 保存到缓冲区

git add .(|git add filename)

7. 添加信息

# Tell the repository your email address and user name
git config --global user.email "you@example.com"
git config --global user.name "Your Name"

8. 添加描述

# Add submission file description
git commit -m 'your description'

9. Push本地文件到远程仓库

# Push the local repository to the remote repository
git push origin master

note:If 'Could not resolve host' appears, It could be a system agent, you need to cancel it.

git config --global --unset http.proxy

unset http_proxy
http_proxy=""
unset https_proxy
https_proxy=""

日常操作所用命令

  • 克隆仓库

    git clone <git地址>
    

    初始化仓库:

    git init
    
  • 添加文件到暂存区

    git add -A
    

    把暂存区的文件提交到仓库:

    git commit -m "提交信息"
    

    查看提交的历史记录:

    git log --stat
    
  • 工作区回滚

    git checkout <filename>
    

    撤销最后一次提交:

    git reset HEAD^1
    
  • 以当前分支为基础新建分支

    git checkout -b <branchname>
    

    列举所有的分支:

    git branch
    

    单纯地切换到某个分支:

    git checkout <branchname>
    

    删掉特定的分支:

    git branch -D <branchname>
    

    合并分支:

    git merge <branchname>
    
  • 推送当前分支最新的提交到远程

    git push
    

    拉取远程分支最新的提交到本地:

    git pull