Git 和 GitHub 使用经验积累

本文主要介绍了 Git 和 GitHub 使用的一些基本知识,包括 Git 的基础知识,GitHub 的基础知识,GitHub 的工作流程,GitHub 使用注意事项等。

Git Basics

Git Commands

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
git init # Initialize git repository
git add <name> # Add changed file
git commit -m "comments" # Add comments about modification
git push -u origin main # Push changes to the main branch
git pull origin main # Fetch remote repository and merge with local branch

git branch -M main # Forced branch change
git branch -v # Show sha1 and commit subject line for each head
git remote add origin https://github.com/xxxxx/nvim.git # Set a remote with name <origin> with url <https://github.com/OWNER/REPOSITORY.git>
git remote remove <name> # Remove remote repository
git remote -v # Show remote repositories

git tag -a version -m "comments" # Create tag
git push origin --tags # Push tags

git reset --hard # Resets the index and working tree
git reset --hard <head> # Revert to previous <head> version

Get Started

Authentication

Repositories

Repositories - GitHub Docs

GitHub 工作流程

  • 安装 Git

  • 全局配置

    设置 Git 的 username 和 email,这个设置是为了让 git 知道是谁对文件进行了更改,所以这里的设置是全局的设置。
    在任意文件夹处 Git Bash Here:

    1
    2
    3
    4
    git config --global user.name "username" # 配置用户名
    git config --global user.email "username@mail.com" # 配置邮箱
    git config --global init.defaultBranch main # 设置默认分支
    git config --list --show-origin --show-scope # 查看配置
  • 通过 SSH 方式连接 GitHub

    ssh fails to start due to missing host keys

  • Github 上新建仓库

    这里需要在 GitHub 上新建一个 repository,需要注意的是本地文件夹的名字和 GitHub 上的 repository 的名字一样。GitHub 新建了一个 repository 之后会有下面的命令展示。

    1
    2
    3
    4
    5
    6
    7
    echo "# example " >> README.md
    git init
    git add README.md
    git commit -m "first commit"
    git branch -M main
    git remote add origin https://github.com/xxxxx/example.git
    git push -u origin main
  • Push 代码到 Github

    进入要 push 代码的目录下,Git Bash Here

    1
    2
    3
    4
    5
    6
    7
    8
    git init # 初始化
    git add . # 将所有改动添加到暂存区 (也可以单独加载 git add README.md)
    git commit -m "提交说明" # 提交文件,创建时间点,添加注释
    git branch -M main
    git status # 查看 git 的状态
    git remote add origin https://github.com/username/repository.git # 初次使用:添加远程的代码库到配置
    git push -u origin main # 将本地更改推送到远程master分支。
    git pull origin main # 将远程的更改拉取到本地

GitHub 使用注意事项

  • GitHub 上只能查看,不要去更改。
  • 本地文件夹和 repository 的名称要相同。

Git and GitHub Tutorial - Version Control for Beginners

Git 过滤文件,控制上传文件类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
touch .gitignore
echo "*.aux" >>.gitignore # (>> 是在文件尾增加,> 是删除已经存在的内容再增加)

/表示目录
比如/A/*就是忽略A目录下所有内容

*表示匹配多个字符
忽略以iml结尾的文件件就用*.iml

[]表示匹配多个单个字符
[abc] 就是代表a,b,c中的任意一个字符就好

!表示跟踪某类文件
比如 /*,</

Git 和 GitHub 使用经验积累

https://latexalpha.github.io/5eebb06843d1/

作者

Shangyu ZHAO

发布于

2020-03-01

更新于

2024-04-19

许可协议