项目用git命令配置git_日常工作流程中使用的常见git命令和配置

项目用git命令配置git

This article was originally published at https://zean.be/articles/git-commands

本文最初发表于https://zean.be/articles/git-commands

I’m a command line lover and use Git from the terminal all the time.

我是命令行爱好者,一直在终端上使用Git。

If you’re like me, you might start getting annoyed by some little things at a certain point. For example, it starts to get a bit annoying to always type two separate commands — git add <file> then git commit -m 'Your commit message' - to commit your changes. Or maybe you want to have a better looking git history when you type git log. Or you want your local branch to be automatically pruned when the remote branch has been deleted. Little things like these - you get the idea.

如果您像我,可能会在某个时候开始被一些小事情烦恼。 例如,总是输入两个单独的命令开始变得有点烦人: git add <file>然后git commit -m 'Your commit message'提交更改。 或者,也许您希望在键入git log时拥有更好的git历史git log 。 或者,您希望删除远程分支后自动修剪本地分支。 像这样的小事情-您明白了。

Over time, I have built up a curated list of commands, aliases and configurations that I use on a daily basis that makes my workflow more efficient and pleasant. And I’d like to share them with you below.

随着时间的流逝,我建立了精选的命令,别名和配置清单,这些清单每天都会使用,以使我的工作流程更高效,更愉快。 我想在下面与您分享。

列出您的本地分支机构及其远程跟踪分支机构 (List your local branches and their remote tracking branches)

In addition, this command also shows the hash code and the commit message of the latest commit. It also tells you if the remote branch has been deleted.

另外,此命令还显示哈希码和最新提交的提交消息。 它还会告诉您是否已删除远程分支。

git branch -vv

For example, running the command produces the following output on my machine,

例如,运行命令在我的机器上产生以下输出,

Image for post
List all branches in Git
列出Git中的所有分支

检出远程分支 (Checkout a remote branch)

With Git versions ≥ 1.6.6 and with only one remote, you can just do:

在Git版本≥1.6.6且只有一个遥控器的情况下,您可以执行以下操作:

git fetch
git checkout <branch_name>

git checkout <branch_name> will NOT work in modern Git if you have multiple remotes. In this case use

git checkout <branch_name>不会在现代Git的工作,如果你有多个遥控器。 在这种情况下使用

git checkout -b <branch_name> <remote_name>/<branch_name>

or the shorthand

或速记

git checkout -t <remote_name>/<branch_name>

添加并提交一个命令 (Add and commit in one command)

Add either one of the following aliases to your global Git config file (usually at ~/.gitconfig on a Linux/Mac OS system). I prefer the second one because it saves a few more keystrokes.

将以下别名之一添加到全局Git配置文件中(通常在Linux / Mac OS系统上为~/.gitconfig )。 我喜欢第二个,因为它可以节省更多的击键。

# add a `add-commit` alias, or
git config --global alias.add-commit '!git add -A && git commit'
# add a `ac` alias to save a few more keystrokes
git config --global alias.ac '!git add -A && git commit'

And use it with

并与

git add-commit -m 'My commit message' # or
git ac -m 'My commit message'

在本地和远程删除分支 (Delete a branch both locally and remotely)

When you’re done with a branch, you can delete it from both the remote and your local machine using the commands below.

完成分支后,可以使用以下命令从远程计算机和本地计算机中将其删除。

# delete a remote branch
git push -d <remote_name> <branch_name> # or
git push -D <remote_name> <branch_name>
# delete a local branch
git branch -d <branch_name> # or
git branch -D <branch_name>

Note that in most cases the <remote_name> name is origin.

请注意,在大多数情况下, <remote_name>名称是起源。

Note: The -d option is an alias for --delete, which only deletes the branch if it has already been fully merged in its upstream branch. You could also use -D, which is an alias for --delete --force, which deletes the branch "irrespective of its merged status".

注意-d选项是--delete的别名,仅当该分支已在其上游分支中完全合并时才删除该分支。 您也可以使用-D ,它是--delete --force的别名,它将删除分支“ 而不考虑其合并状态 ”。

删除已在远程中合并的所有分支 (Delete all branches that have been merged in remote)

Assume you have a long running development branch, and you branch off it to create different feature branches e.g. feature/A, feature/B.

假设您有一个运行时间很长的development分支,并且将其分支以创建不同的功能分支,例如feature/Afeature/B

After your peers have reviewed your pull requests for both features, merged them back to development and deleted them from remote. You can delete feature/A and feature/B from your local by running,

在您的同辈查看您对这两个功能的拉取请求后,将它们合并回development并从远程删除它们。 您可以通过运行以下命令从本地删除feature/Afeature/B

# switch to the development branch first
git checkout development
# delete local branches whose remote tracking branches have been merged back to development
git delete-merged

You probably have noticed that delete-merged is not a Git command - it's actually an alias we set up. The parameters used in the actual command is different depending on your setup. But you can follow the following steps to construct a command that suits your needs.

您可能已经注意到delete-merged不是Git命令-它实际上是我们设置的别名。 实际命令中使用的参数因您的设置而异。 但是您可以按照以下步骤来构造适合您需要的命令。

Step 1: Check out the development branch.

步骤1 :检出development分支。

Step 2: List all branches that have been merged into it in remote.

第2步 :列出远程已合并到其中的所有分支。

git branch --merged

Step 3: You might see a few branches that you don’t want to remove e.g. master, release etc. And you can filter down the list by excluding those branches by

第3步 :您可能会看到一些不想删除的分支,例如masterrelease等。您可以通过按以下方式排除那些分支来过滤列表

git branch --merged | egrep -v "(^\*|master|development|skip_branch_name)"

The regular expression used by the egrep command basically means "all branches whose name starts with master, development or skip_branch_name will not be deleted".

egrep命令使用的正则表达式基本上意味着“ 不会删除名称以masterdevelopmentskip_branch_name开头的所有分支”。

You can modify the branches above or add your own branches that you don’t want to delete.

您可以修改上面的分支,也可以添加自己不想删除的分支。

Step 4: Delete all local branches that are already merged into the currently checked out branch

步骤4 :删除所有已经合并到当前签出分支中的本地分支

git branch --merged | egrep -v "(^\*|master|development|skip_branch_name)" | xargs git branch -d

Step 5: Set a global alias deleted-merged for the command

步骤5 :为命令设置一个deleted-merged的全局别名

git config --global alias.delete-merged 'git branch --merged | egrep -v "(^\*|master|development|skip_branch_name)" | xargs git branch -d'

丢弃当前工作目录中未暂存的文件 (Discard unstaged files in current working directory)

To discard all unstaged files in current working directory,

要丢弃当前工作目录中所有未暂存的文件,

git checkout -- .

For a specific file, use

对于特定文件,请使用

git checkout -- path/to/file/to/revert

The -- is to remove argument ambiguation.

--是消除参数歧义

重命名本地和远程分支 (Rename a local and remote branch)

Step 1: Rename your local branch

第1步 :重命名您的本地分支机构

If you are on the branch you want to rename:

如果您在分支机构上,则要重命名:

git branch -m <new-name>

If you are on a different branch:

如果您在另一个分支上:

git branch -m <old-name> <new-name>

Step 2: Delete the <old-name> remote branch and push the <new-name> local branch

第2步 :删除<old-name>远程分支并推送<new-name>本地分支

git push origin :<old-name> <new-name>

Step 3: Reset the upstream branch for the <new-name> local branch

步骤3 :为<new-name>本地分支重置上游分支

Switch to the branch and then:

切换到分支,然后:

git push origin -u <new-name>

美化Git日志 (Prettify the Git log)

You can format your Git log to look like below and set an alias for it.

您可以将Git日志的格式设置为如下所示,并为其设置别名。

Image for post
Git Log
Git日志

Step 1: Set up the following alias in your global Git config file

步骤1 :在全局Git配置文件中设置以下别名

git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"

Step 2: Append additional flags if needed

步骤2 :如有需要,附加其他标志

# filter by start date
--after="2016-01-31"
--since="2016-01-31"
# filter by end date
--before="2017-03-10"
--until="2017-03-10"
# filter by author
--author="Zean Qin"

My most commonly used command before a SCRUM meeting is

我在SCRUM会议之前最常用的命令是

git lg --after="yesterday" --author="Zean"

This shows all commits I made yesterday.

这显示了我昨天所做的所有提交。

美化Git差异 (Prettify Git diff)

I use diff-so-fancy to make the diffs more readable. Follow the official setup steps and then just run

我使用diff-so-fancy使diff更具可读性。 遵循官方设置步骤 ,然后运行

git diff

自动修剪另一端已删除的分支的远程跟踪分支 (Prune remote-tracking branches automatically for a branch on the other side that has already been deleted)

Without git fetch --prune, remote-tracking branches for a branch the other side already has removed will stay forever.

如果没有git fetch --prune ,则另一端已经删除的分支的远程跟踪分支将永远存在。

To always --prune for git fetch and git pull in all your Git repositories:

始终--prune进行git fetchgit pull入所有Git存储库:

git config --global fetch.prune true

To always --prune but from one single repository,

要始终--prune从一个存储库中--prune

git config remote.origin.prune true
#^^^^^^
#replace with your repo name

使Git使用Vim作为编写提交消息的编辑器 (Make Git to use Vim as editor for writing commit messages)

If you want to set the editor only for Git, do either (you don’t need both):

如果您只想为Git设置编辑器,请选择其中一种(您无需同时使用):

  • Set core.editor in your Git config: git config --global core.editor "vim"

    在您的Git配置中设置core.editorgit config --global core.editor "vim"

  • Set the GIT_EDITOR environment variable: export GIT_EDITOR=vim

    设置GIT_EDITOR环境变量: export GIT_EDITOR=vim

If you want to set the editor for Git and also other programs, set the standardized VISUAL and EDITOR environment variables:

如果要为Git和其他程序设置编辑器,请设置标准化的VISUALEDITOR环境变量:

Setting both is not necessarily needed, but some programs may not use the more-correct VISUAL.

不一定需要同时设置两者,但是某些程序可能未使用更正确的VISUAL

export VISUAL=vim
export EDITOR="$VISUAL"

翻译自: https://medium.com/@ZeanQin/common-git-commands-and-configurations-used-in-a-day-to-day-workflow-208b9d0ddc8c

项目用git命令配置git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值