蘑菇小姐会开花

git操作笔记

写在前面

最近可能陆续会补一些以前学习时做的笔记吧,算是一个回顾,也是总结一下准备新的开始。
毕竟!再不学习!!就要废了!!!

git操作

我比较常用的一个工作流程大概是这样:

初始化一个git仓库

1
2
// creates a new Git repository
git init

本地仓库修改提交

添加至暂存区

1
2
// adds files from the working directory to the staging area
git add

1
2
git add . // 添加所有改变文件,任何未改变文件都不会被包含
git add index.html // 添加 index.html

查看状态

1
2
// inspects the contents of the working directory and staging area
git status

查看改动

1
2
// shows the difference between the working directory and the staging area 
git diff

提交

1
2
// permanently stores file changes from the staging area in the repository
git commit

云端仓库

A remote is a Git repository that lives outside your Git project folder. Remotes can live on the web, on a shared network or even in a separate folder on your local computer.

云端建立分支

1
git remote add origin git@github.com:username/repository.git

同步云端仓库

1
2
// push a local branch to the origin remote
git push -u origin master

这个流程一般是我在本地建好仓库后同步至github时的操作。还有一个常用的流程是从github克隆代码,然后本地修改上传。

克隆一个git仓库

1
2
// Creates a local copy of a remote
git clone url

版本回退

查看提交纪录

1
2
// shows a list of all previous comits
git log

查看云端项目

1
2
// Lists a Git project's remotes.
git remote -v

回退

1
2
3
4
5
6
7
// backtrack in Git:
// Discards changes in the working directory
git checkout HEAD filename
// Unstages file changes in the staging area
git reset HEAD filename
// Can be used to reset to a previous commit in your commit history.
git reset SHA

git分支

Git branching allows users to experiment with different versions of a project by checking out separate branches to work on.

1
2
// Lists all a Git project's branches.
git branch

1
2
// Creates a new branch.
git branch branch_name
1
2
// Used to switch from one branch to another.
git checkout branch_name
1
2
// Deletes the branch specified.
git branch -d branch_name
1
2
// Used to join file changes from one branch to another.
git merge branch_name

git协作流程

The Git Collaborative Workflow are steps that enable smooth project development when multiple collaborators are working on the same Git project.
The workflow for Git collaborations typically follows this order:
1.Fetch and merge changes from the remote

1
2
// Fetches work from the remote into the local copy.
git fetch

1
2
// Merges origin/master into your local branch.
git merge origin/master

2.Create a branch to work on a new project feature
3.Develop the feature on your branch and commit your work
4.Fetch and merge from the remote again (in case new commits were made while you were working)
5.Push your branch up to the remote for review

坚持原创技术分享,您的支持将鼓励我继续创作!