1

我是 git 新手。我知道一旦我使用添加新文件git add,我必须运行git commit然后git push

前几天,我做了这个,惊讶地发现并不是我所有的文件都被签入了,所以我想问以下问题:

一旦我这样做git commit了,我如何才能在我做之前确切地知道要检查什么git push?我可以复制我的项目,因为它将被推送到 git 吗?

4

6 回答 6

7

如果你在它git status之前运行git commit它会告诉你哪些更改将包含在提交中,哪些不会。

运行git commit后,更改已签入本地存储库。推送不会将它们签入,它只是使用您在本地存储库中的提交更新另一个存储库。这与像 Subversion 这样的集中式 VCS 不同。

在推送之前,有几种方法可以查看将要推送的内容:

  • git diff origin/master将显示您当前状态和origin/master.
  • git log origin/master..master将列出将被推送的提交,但不显示它们的内容。
于 2013-06-27T15:37:46.040 回答
3
git show

将显示提交最后一次提交的详细信息。

如果有多个提交,您也可以使用

git diff origin/master

假设您的遥控器已命名origin,并且您要推送到的分支是master.

于 2013-06-27T15:41:45.693 回答
2
  1. git diff将使您能够很好地处理所做的更改。例如,您可以使用git diff origin/master(如果您推送到的远程名为 origin)来查看当前分支和远程之间的差异。

  2. 另一种方法是使用git show. 如果您已在本地进行了 3 次提交,则可以使用git show HEAD...HEAD~3查看最近 3 次提交的内容。

  3. 一种更复杂(但易于别名)的方法是使用whatchanged

    git whatchanged -p --abbrev-commit --pretty=medium

    gwc我在我的 shell 中使用了一个 git 别名填充。

于 2013-06-27T15:37:52.780 回答
1

为此,我使用 bash 脚本的别名:

我将下面的脚本保存~/bin/git-outgoing在我的主目录中的目录中。我~/bin/的在我的PATH;如果您打算为其设置别名,请确保您可以访问它。

git-outgoing

#!/bin/sh
# Usage: git-outgoing [<upstream>] [<head> [<limit>]]
# Show commits on current branch that do not exist on branch <upstream>.

# bail out with message to stderr and exit status 1
die() {
    echo "$(basename $0):" "$@" 1>&2
    exit 1
}

# colors
SHA=$(git config --get-color 'color.branch.local')
ADD=$(git config --get-color 'color.diff.new')
REM=$(git config --get-color 'color.diff.old')
RESET=$(git config --get-color '' 'reset')

# get the current branch in refs/heads/<branch> form
ref=$(git symbolic-ref -q HEAD)
test -n "$ref" ||
die "you're not on a branch"

# just the branch name please
branch=$(echo "$ref" | sed 's@^refs/heads/@@')
test -n "$branch" ||
die "you're in a weird place; get on a local branch"

# use tracking branch if no upstream given
if [ $# -eq 0 ]
then
    remote=$(git config --get "branch.$branch.remote" || true)

    merge=$(git config branch.$branch.merge) ||
    die "branch $branch isn't tracking a remote branch and no <upstream> given"

    set -- "$remote/$(echo "$merge" |sed 's@^refs/heads/@@')"
fi

git cherry -v "$@"                                |
cut -c1-9,43-                                     |
sed -e "s/^\(.\) \(.......\)/\1 $SHA\2$RESET/"    |
sed -e "s/^-/$REM-$RESET/" -e "s/^+/$ADD+$RESET/"

这个随附的入站脚本进入~/bin/git-incoming

git 传入

#!/bin/sh
# Usage: git-incoming [--diff] [<upstream>] [<head> [<limit>]]
# Show commits on <upstream> that do not exist on current branch.

# bail out with message to stderr and exit status 1
die() {
    echo "$(basename $0):" "$@" 1>&2
    exit 1
}

# colors
SHA=$(git config --get-color 'color.branch.local')
ADD=$(git config --get-color 'color.diff.new')
REM=$(git config --get-color 'color.diff.old')
RESET=$(git config --get-color '' 'reset')

# check for -d / --diff argument
diff=false
if [ "$1" = '-d' -o "$1" = '--diff' ]
then diff=true
     shift
fi

# use tracking branch if no upstream given
if [ $# -eq 0 ]
then
    # get the current branch in refs/heads/<branch> form
    ref=$(git symbolic-ref -q HEAD)
    test -n "$ref" ||
    die "you're not on a branch"

    # just the branch name please
    branch=$(echo "$ref" | sed 's@^refs/heads/@@')
    test -n "$branch" ||
    die "you're in a weird place; get on a local branch"

    # grab remote name for current branch
    remote=$(git config --get "branch.$branch.remote" || true)

    # grab tracked branch name for current branch
    merge=$(git config branch.$branch.merge) ||
    die "branch $branch isn't tracking a remote branch and no <upstream> given"

    # make it so
    set -- "$remote/$(echo "$merge" |sed 's@^refs/heads/@@')"
fi

if $diff
then git diff HEAD..."$1"
else
     git cherry -v HEAD "$@"                           |
     cut -c1-9,43-                                     |
     sed -e "s/^\(.\) \(.......\)/\1 $SHA\2$RESET/"    |
     sed -e "s/^-/$REM-$RESET/" -e "s/^+/$ADD+$RESET/"
fi

在我的.gitconfig中,我为它创建了一个别名,如下所示:

[alias]
out = !git-outgoing
in = !git-incoming

现在我可以git out在我的项目根目录中查看将被推送的内容,或者git in查看将被拉取的内容。

这些是我发现比我自己的脚本更好的脚本。我找不到作者,但会在我找到时编辑。

于 2013-06-27T15:53:13.413 回答
0

您可以通过多种方式进行操作,但最简单的是使用git log --stat. 它将向您显示更改的文件的摘要:

$ git log -2 --stat
commit 85318f521f6c0b9843d6da12abf67f2de7608431
Author: Junio C Hamano <gitster@pobox.com>
Date:   Wed Jun 26 15:10:17 2013 -0700

    Update draft release notes to 1.8.4

    Signed-off-by: Junio C Hamano <gitster@pobox.com>

 Documentation/RelNotes/1.8.4.txt | 12 ++++++++++++
 1 file changed, 12 insertions(+)

commit ad76feb55eb7645661421e213796129efcbb7d56
Merge: 12dd2f6 212eb96
Author: Junio C Hamano <gitster@pobox.com>
Date:   Wed Jun 26 15:08:09 2013 -0700

    Merge branch 'tr/maint-apply-non-git-patch-parsefix'

    Fix for the codepath to parse patches that add new files, generated
    by programs other than Git.  THis is an old breakage in v1.7.11 and
    will need to be merged down to the maintanance tracks.

    * tr/maint-apply-non-git-patch-parsefix:
      apply: carefully strdup a possibly-NULL name

这是来自 git 存储库的示例。注意:默认情况下,合并提交不会显示任何内容。如果您想查看它们的统计信息,您需要传递--cc选项(代表“紧凑组合”):

$ git log -2 --stat --cc
commit 85318f521f6c0b9843d6da12abf67f2de7608431
Author: Junio C Hamano <gitster@pobox.com>
Date:   Wed Jun 26 15:10:17 2013 -0700

    Update draft release notes to 1.8.4

    Signed-off-by: Junio C Hamano <gitster@pobox.com>

 Documentation/RelNotes/1.8.4.txt | 12 ++++++++++++
 1 file changed, 12 insertions(+)

commit ad76feb55eb7645661421e213796129efcbb7d56
Merge: 12dd2f6 212eb96
Author: Junio C Hamano <gitster@pobox.com>
Date:   Wed Jun 26 15:08:09 2013 -0700

    Merge branch 'tr/maint-apply-non-git-patch-parsefix'

    Fix for the codepath to parse patches that add new files, generated
    by programs other than Git.  THis is an old breakage in v1.7.11 and
    will need to be merged down to the maintanance tracks.

    * tr/maint-apply-non-git-patch-parsefix:
      apply: carefully strdup a possibly-NULL name

 builtin/apply.c         |  2 +-
 t/t4111-apply-subdir.sh | 14 ++++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)

要查看实际更改的内容,您可以使用-pon 选项git log,或直接使用 . 查看提交git show COMMIT_ID。我更喜欢后者。 git show也接受该--stat选项。

于 2013-06-27T15:47:39.277 回答
0

启动gitk或任何其他可视化历史浏览器:它显示您的分支和来源、提交中的差异,并提供所有其他工具来进行事后审查。

于 2013-06-27T16:25:09.057 回答