Git - Practical commands

记录自己常用到的Git命令行。

Setting up a repository

git init

This is usually the first command you’ll run in a new project. 该命令通常是新项目中你会运行的第一个命令。

它可以被用来转化一个 existing, unversioned project (已经存在还没有版本的项目)到Git仓库中,或者 initialize a new empty repository(初始化一个全新的空的Git仓库)。

This command creates a .git subdirectory in the project root, which contains all of the necessary metadata for the repo, and makes it possible to start recording revisions of the project.

该命令在项目根目录里创建了这个子目录。使得它可以开始记录项目的版本。

注意:Git will not create amaster branch until you commit something. Add one or more files to your directory, andgit add them to prepare a commit. Then git commit to create your initial commit and master branch.

随便在目录里添加一个文件,添加并提交,此时才真正完成master分支的创建。

git clone

This command copies an existing Git repository. 如果项目已经有人开始做,此时需要把远端刀repository复制到本地。Cloning automatically creates a remote connection called origin pointing back to the original repository.

Inspecting a repository

git status

This command displays the state of the working directory and the staging area. List which files are staged, unstaged, and untracked.

该命令显示工作分支和临时集结区域的状态,哪些文件集结了,没有被集结,还是没有被追踪。

Saving changes

git diff <filename>

比较当前文件和集结区文件的诧异,也可以不指定文件名,这样会显示所有的诧异。

git add <filename>

Stage all changes in <file> for the next commit. 为下一个提交/托付集结所有 <file> 中的改变。

git commit

弹出一个文本编辑器用来编辑提交信息

git commit -m "Commit message"

First, you edit your files in the working directory. When you’re ready to save a copy of the current state of the project, you stage changes with git add. After you’re happy with the staged snapshot, you commit it to the project history with git commit.

首先你在工作分支编辑文件。当你准备保存项目当前状态的副本时,使用 git add 集结这些改变。然后当你对集结的快照感到满意时,使用 git commit 提交/托付到项目历史中。

git stash

当你正在一个分支做新功能的时候,发现需要在另一个分支修改bug,使用该指令把你当前做的工作(还没有达到 commit 的程度)保存到一个安全的地方,然后等你在其他分支完成任务回来后,再把他们取出来继续做。

git stash list

查看暂存列表

git stash pop stash@{num}

num是你希望恢复的暂存操作序号,通过列表指令可以查看。缺省 stash@{num} 则执行恢复 stash 队列中 stash@{0} 的记录。

git stash clear

清空 stash 记录

Rewriting history

git commit --amend

This command provide a convenient way to fix up the most recent commit. It lets you combine staged changes with the previous commit instead of committing it as an entirely new snapshot. It can also be used to simply edit the previous commit message without changing its snapshot.

该指令提供了一种修补最新提交的简便方法。使你能够把新集结的改变 (通过git add)结合到前一个提交中,而不需要创建一个全新的快照来提交。也可以在不改变快照的情况下,单纯用来修改前一个提交的message。

git rebase

一种好的工作方式是,在把新功能的commit推到远端之前,检查一下主分支有没有更新,有的话,把更新合并到这个提交里,并在本地把冲突解决掉,这样别人在远端code review时,省掉了许多麻烦。

假设已经运行 git pull <remote> <本地主分支>, git checkout <新功能分支>,然后分两种情况:

1. 新功能还没有推到远端。

git rebase <本地主分支>

如果有冲突,解决冲突 → save file → git add → git rebase --continue

2. 新功能分支已经推到远端

git merge <本地主分支>

如果有冲突,解决冲突 → save file → git add → git commit

两者的区别就是,前者并不创建新提交。

Using Branches

By developing features in branches, it’s not only possible to work on both of them in parallel, but it also keeps the main master branch free from questionable code.

通过在分支里开发功能,不仅使同时开发不同功能成为可能,而且保持主master分支不会遭到有问题代码的伤害。

git branch

List all of the branches in your repository. 列出仓库中所有分支。

The current branch will be highlighted with an asterisk 星号(*). 当前所在分支前以星号标记。

git branch <branch_name>

Create a new branch called <branch_name>. 以任务名称创建一个新分支。

git branch -d <branch_name>

Delete the specified branch. This is a “safe” operation in that Git prevents you from deleting the branch if it has unmerged changes.

删除指定分支,如果该分支还没有merge到当前分支,则提示error。若要强制删除,使用 -D

git branch -m <branch_new_name>

Rename the current branch to <branch_new_name>. 当前分支重命名。

git branch -r

To view your remote branches, remote branches are prefixed by the remote they belong to.

查看你在远端的分支,远端分支由他们所属的分支名开始,以便与本地区分。

git checkout <existing-branch>

Check out the specified branch, which should have already been created with git branch. This makes <existing-branch> the current branch, and updates the working directory to match.

跳转到已经通过 git branch 创建的指定分支上,使得它成为当前分支。

git checkout -b <new-branch>

Create and check out <new-branch>. 加 -b 选项可以告诉Git先创建再跳转。

Syncing

git remote

List the remote connections you have to other repositories. 列出你与其他仓库的远端链接

git fetch <remote>

Fetch all of the branches from the repository. This also downloads all of the required commits and files from the other repository. Since fetched content is represented as a remote branch, it has absolutely no effect on your local development work. This makes fetching a safe way to review commits before integrating them with your local repository.

把远端仓库的分支都取下来,取下来的内容也被视为远端分支,所以不会影响你本地开发工作。与人合作同一个任务时,先运行 git fetch 命令,把他的分支取下来,然后 git checkout <分之名>,在本地创建同名新分支并跳转过去。

git pull <remote>

Fetch the specified remote’s copy of the current branch and immediately merge it into the local copy. This is thesame asgit fetch <remote> followed bygit merge origin/<current-branch>.

取得指定的在远端的当前分支的副本,然后立刻把它合并到当前副本。

git push <remote> <branch>

Push the specified branch to <remote>, along with all of the necessary commits and internal objects. This creates a local branch in the destination repository. To prevent you from overwriting commits, Git won’t let you push when it results in a non-fast-forward merge in the destination repository.

把指定分支推到远端,通过该指令把本地仓库的提交传到远端仓库。该指令在目的仓库创建一个本地分支,以阻止你重写提交,在远端仓库造成冲突。

Undoing Changes

git reset --hard <commit>

Move the current branch tip backward to <commit> and reset both the staging area and the working directory to match.

把当前分支返回到指定hashID的提交,重置暂存/集结区和工作目录。


Git文件状态

分为 untracked tracked.

untracked 文件是指新建的尚未被 git 管理起来的文件。

tracked 又分为三种状态:

已修改( modified ) 表示修改了但没有提交保存;

已暂存( staged ) 表示把已修改的文件放在下次提交时要保存的清单中;

已提交( committed ) 表示文件已被安全地保存在本地仓库中了。

Bonus:

Gitk 一个基于 Tcl/Tk 的 Git 浏览器 (For Unix/Linux)
主要用于用户查看仓库的各类信息(更改信息、提交信息、版本信息、图形显示等)

使用emacs来编写 git commit 注释

Git默认会调用你的环境变量 editor 定义的值作为文本编辑器,如果没有定义,则调用Vi来创建和编辑提交以及标签信息, 可以使用 core.editor 改变默认编辑器:

git config --global core.editor emacs

这样git commit 会自动打开你刚才指定的编辑器。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值