这是我参与「第三届青训营 -后端场」笔记创作活动的的第2篇笔记.
我打算在这总结一些GIT的一些指令方便查阅.
GIT 指令
- echo指令 echo指令会将用户输入的内容存入指定的文件中,一共有两种方法.
echo content > file //将输入内容存入文件内容的尾部
echo content >> file //将输入内容直接覆盖文件内容
- git init 创建一个新的git 仓库. 使用git init 有很多参数可以选择, 如下.
git init [-q | --quiet] [--bare] [--template=<template-directory>]
[--separate-git-dir <git-dir>] [--object-format=<format>]
[-b <branch-name> | --initial-branch=<branch-name>]
[--shared[=<permissions>]] [<directory>]
//see https://git-scm.com/docs/git-init
- git add <file name> 添加文件到staging area, 当文件被加到staging area之后就可以准备commit了.
- git clone
git add hello.go // 添加hello.go到staging area
git add . //添加所有更改过的文件到staging area
- git commit 使用git commit之后可以将staging area的文件存入当前主机内的仓库, git分为远程仓库和本地仓库. 远程仓库就是github, bitbucket之类的网站可以供我们存储自己的代码,本地仓库就是存在我们使用的主机里的仓库. 只有当代码存到本地仓库之后才可以上传到远程仓库.
git commit -m "some messages"
- git status 获取当前仓库内文件的修改信息和状态 (staging, not staging or commited). -git pull 将远程仓库已有的内容拉到自己的的仓库中 -git fetch 和git pull的功能相似但是不会强制merge冲突的内容
- git push 将本地仓库中已经commit的内容上传到远程仓库.
- git branch 当我们使用git时,我们可以创建自己独立的分支,这样子在修改自己的代码时不会与别人的代码发生冲突.
git branch // list all branches
git branch <branch name> // create a new branch
git branch -d <branch name> // safe delete (prevent deleting the branch with unmerged changes>
git branch -D <branch name> //force delete
git branch -m <branch name> //rename the current branch to another name
git branch -a // list all remote branches
- git merge 当我们需要将两个不同的分支branch_a和branch_b合并起来的时候,我们需要用到merge指令. 假设现在指针指向branch_a, 当我们输入 git merge branch_b, brach_b会被合并到branch_a.
- merge conflict 通常,当我们使用git pull指令时, git会自动帮我们进行文件比较,所以大部分时候我们需要自己手动进行git merge. 但我们和小伙伴修改了同一行代码时, git会询问我们如何进行操作, 因为它不能够判断是否对于冲突的代码部分只保留一部分代码还是全部保留.
<< Head
Some Content
>>> new_branch_to_merge
Some content
- git checkout 让用户可以在自己建立的分支里切换
git checkout <branch name> //切换分支
git checkout -b <branch name> //创建分支并切换
- git reset & git revert
两种指令都可以将当前分支退回指定分支.
git reset会将独立的分支中已经做过的修改undo然后返回指定的commit.
git revert会将公共的分支中已经做过的修改undo然后返回指定的commit.