Git is a distributed version control system. Git is free software.
- 初始化Git仓库,用
git init
命令。 - 添加文件到Git仓库
git add file1.txt file2.txt
,追踪文件。git commit
提交变更。- Commit message format:
<commit summary in 50 characters or less.> <blank line> <detailed description of changes in this commit.>
git status
查看工作区状态。git diff 查看unstaged
的更改内容。git diff –cached
查看staged更改内容。
git reset --hard HEAD^
回退到上一个版本git reset --hard HEAD~n
回退到前n个版本⬇git reset --hard commit_id
回到指定id的版本git log
查看当前版本之前的提交历史,以便确定要回退到哪个版本git reflog
查看命令历史,可以看到所有提交版本的记录
- 工作区:workspace, working directory
- 暂存区:stage
- 版本库:repository
@import "git_add.png"
@import "git_commit.png"
git add
命令实际上是把要提交的所有更改放到暂存区——stage。git commit
则把暂存区的内容全部提交到分支
To delete a file from a project, you need to add it to the staging area just like a new or modified file. The next command will stage the deletion and stop tracking the file, but it won’t delete the file from the working directory:
git rm --cached <file>
A natural grouping of commands:
- Stage/Working Directory:
git add
git rm
git status
- Committed History:
git commit
,git log
To delete a file from a project, you need to add it to the staging area just like a new or modified file. The next command will stage the deletion and stop tracking the file, but it won’t delete the file from the working directory:
git rm --cached <file>
A natural grouping of commands:
- Stage/Working Directory: git add, git rm, git status
- Committed History: git commit, git log
- "为什么Git比其他版本控制系统设计得优秀,因为Git跟踪并管理的是修改,而非文件。"
- 没有
git add
到 stage 的更改不会被commit
到版本库中。
Git 保存的不是文件的变化或者差异,而是一系列不同时刻的文件快照。1
use git checkout -- <file>...
to discard changes in working directory
git checkout -- <file>
命令中的--
很重要,没有--
,就变成了“切换到另一个分支”的命令。
如果<file>
自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一样的状态;
如果<file>
的更改已经添加到暂存区,之后又作了其它修改,现在,撤销修改就回到暂存区中的状态。
use git reset HEAD <file>...
to unstage
git reset
命令既可以回退版本,也可以把暂存区的修改回退到工作区:修改内容退回工作区,没有stage。
git reset HEAD <file>
git checkout -- <file>
git reset --hard HEAD
将工作区和暂存区重置到最近一次提交版本。⬆
git checkout commit id <file>
版本库不受影响,仅仅是将工作区文件设置为某一个版本。
git reset HEAD <file>
Omitting the --hard
flag tells Git to leave the working directory alone (opposed to git reset –-hard HEAD
, which resets every file in both the working directory and the stage).
The staged version of the file matches HEAD, and the working directory retains the modified version.
As you might expect, this results in an unstaged modification in your git status output.
git reset HEAD~n
By moving the HEAD reference backward, you’re effectively removing the most recent commit from the project’s history.
向前移动 n 位 HEAD 指针位置,等效于删除了 n 位之后的版本(版本库中看不见了,实际文件快照还在)。
git revert <commit-id>
用旧版本作为下一个提交版本。
git commit –amend
增加更改,并提交到最近一个版本。
- 从版本库中删除文件
git rm <file>
git commit
- 从版本库恢复工作区中被删除文件
git checkout -- <file>
git checkout
其实是用版本库里的版本替换工作区的版本,无论工作区是更改还是删除,都可以“一键还原”。
- ssh-keygen -t rsa -b 4096 -C "[email protected]"
- Adding a new SSH key to your GitHub account
To add a new remote, use the git remote add
command on the terminal, in the directory your repository is stored at.
The git remote add command takes two arguments:
- A remote name, for example,
origin
- A remote URL, for example,
https://github.com/user/repo.git
- A remote SSH, for exampel,
[email protected]:user/repo.git
for example:
git remote add origin https://github.com/user/repo.git
# Set a new remote with https
git remote add origin git@server-name:path/repo-name.git
# Set a new remote with SSH
git remote -v
# Verify new remote
关联之后,使用命令git push -u origin master
第一次推送master分支的所有内容;此后,用git push origin master
推送最新更改。
Pushing to a remote
Use git push to push commits made on your local branch to a remote repository.
The git push command takes two arguments:
- A remote name, for example,
origin
- A branch name, for example,
master
For example:
git push <REMOTENAME> <BRANCHNAME>
To grab a complete copy of another user's repository, use git clone like this:
git clone https://github.com/USERNAME/REPOSITORY.git