简体   繁体   English

如何在Git Push中修改文件?

[英]How to get the files modified in a Git Push?

I want to get all the files that has been modified in the last Push. 我想获取上次推送中已修改的所有文件。

The Push can have multiple commits. 推送可以有多个提交。

Currently I am able to to get the Modified files of last commit by this command: 目前,我可以通过以下命令获取上次提交的修改文件:

git diff-tree --no-commit-id --name-only -r HEAD

You were almost there. 你快到了 One can achieve this by comparing the current HEAD to the origin branch. 可以通过将当前的HEAD与origin分支进行比较来实现这一目标。 git diff-tree --no-commit-id --name-only -r HEAD..origin/master

Example

git checkout master
git pull
touch addnewfile.txt
git add -A
git commit -am "added new empty file"
echo 'test' > addexistingfile.txt
git commit -am "added test in existing file"
git diff-tree --no-commit-id --name-only -r HEAD..origin/master

The last command shows then the output: 最后一条命令显示输出:

addnewfile.txt
addexistingfile.txt

git push doesn't modify any files. git push不会修改任何文件。

git push adds or removes commits . git push添加或删除提交 The commits have snapshots of files. 提交具有文件快照。 The commits do not have changes, they just have files. 提交没有更改,只有文件。

Here is a file: 这是一个文件:

$ cat name-of-file
I am the contents of file name-of-file.

What are the changes I made to this file? 我对此文件做了哪些更改 I've given you a snapshot , and my question to you is: what are the changes? 我给了您一个快照 ,我的问题是:进行了哪些更改? What key piece of information have I left out of my question? 我遗漏了哪些关键信息?

Think about this for a bit, then read on: Given two snapshots, how will you find changes? 仔细考虑一下,然后继续阅读:给定两个快照,您将如何找到更改? Does git diff help? git diff帮助吗?

Which two snapshots would you like to use? 您要使用哪两个快照?

The git diff command can show you what has changed between any two snapshots: git diff命令可以显示两个快照之间的变化

git diff <hash#1> <hash#2>

Note that you must pick two commit hashes. 请注意,您必须选择两个提交哈希。 However, you can pick those commit hashes by names . 但是,您可以按name选择那些提交哈希。 In some existing Git repository, run: 在一些现有的Git存储库中,运行:

git rev-parse master

and see which commit hash comes out. 并查看出现哪个提交哈希。 Then run git log master . 然后运行git log master What's common between the output of git rev-parse and the first line of git log ? git rev-parse的输出和git log的第一行之间有什么共同点?

There's a complete (if overwhelming) list of ways to produce Git hash IDs in the gitrevisions documentation , but the most useful ones for everyday work are branch names like master , tag names like v2.1 , and remote-tracking names like origin/master . gitrevisions文档中提供了完整的(如果压倒一切的)方式来生成Git哈希ID,但是对于日常工作而言,最有用的是分支名称(例如master ,标签名称(例如v2.1 )和远程跟踪名称(例如origin/master The name HEAD means the current commit , 1 and you can abbreviate it @ if you like. 该名HEAD表示当前犯 ,1,您可以缩写它@如果你喜欢。


1 The name HEAD also means the current branch name . 1名称HEAD 表示当前的分支名称 The way Git decides whether you've used HEAD to talk about a commit hash ID or to talk about a branch name is that some commands need a branch name, and others need a commit hash ID. Git决定使用HEAD谈论提交哈希ID还是谈论分支名称的方式是,某些命令需要分支名称,而其他命令则需要提交哈希ID。 The git rev-parse command can ask either question, but the default is to find a hash ID. git rev-parse命令可以询问任何一个问题,但是默认值是查找哈希ID。 You can use git rev-parse --symbolic-full-name HEAD to ask about the branch name, or, if you're extra-serious, you can use git symbolic-ref HEAD for this case. 您可以使用git rev-parse --symbolic-full-name HEAD询问分支名称,或者,如果您非常认真,可以在这种情况下使用git symbolic-ref HEAD


git diff-tree is the plumbing version of git diff for two commits git diff-tree是两次提交的git diff的管道版本

The command you identified, git diff-tree , is a way to run git diff from another computer program. 您标识的命令git diff-tree是从另一台计算机程序运行git diff一种方法。 If you're running git diff to view the difference yourself, on your own computer screen, you would normally use what Git calls a porcelain command, named after the more-polished, user-oriented parts of a bathroom like the sink. 如果您正在运行git diff来自己查看差异,则通常会使用Git所谓的瓷器命令,该命令以更抛光,面向用户的浴室部分(如水槽)命名。 If you're running git diff to get differences to feed to another computer program, you might need to disassemble and re-assemble various equipment along the way, doing what Git calls plumbing , and here you would use git diff-tree to compare two specific commits. 如果您正在运行git diff来获取差异以馈送到另一个计算机程序,则可能需要在此过程中分解并重新组装各种设备,执行Git所谓的plumbing ,在这里您将使用git diff-tree比较两个具体的提交。

The git diff-tree command has one peculiar extra feature: if you run it with just one commit hash ID, or a name like HEAD that resolves to such a hash ID, Git will extract, from that commit, the list of parent commits. git diff-tree命令具有一个特殊的额外功能:如果仅使用一个提交哈希ID或名称(如HEAD解析为这样的哈希ID来运行它,Git将从该提交中提取提交列表。 Git will then diff the commit's parent(s) vs that commit. 然后,Git将比较提交的父对象与该提交。 That's what you are seeing when you run git diff-tree options HEAD . 这就是运行git diff-tree options HEAD时看到的。

To select two commits to compare, simply use both hash IDs, or names for them, on the command line. 要选择两个提交进行比较,只需在命令行上使用两个哈希ID或名称即可。

Plain git diff can also compare other things 普通git diff也可以比较其他东西

The porcelain git diff command can invoke, instead of git diff-tree , the git diff-index or git diff-files plumbing commands. 瓷器git diff命令可以代替git diff-tree来调用git diff-indexgit diff-files plumbing命令。 These allow you to compare a commit's contents to the contents of the index , or a commit to the work-tree, or the index to the work-tree. 这些使您可以将提交的内容与索引的内容进行比较,或者将提交的内容与工作树的内容进行比较,或者将提交的内容与工作树的索引进行比较。

Commits, the index, and the work-tree 提交,索引和工作树

Remember that at all times, Git has three active copies of each file: 请记住,Git始终具有每个文件的三个活动副本:

  • The frozen copy in the HEAD commit is available through git show HEAD: path . 可以通过git show HEAD: path获得HEAD提交中的冻结副本。
  • The work-tree copy is an ordinary file that you can see and edit. 工作树副本是您可以查看和编辑的普通文件。 Git doesn't really use this file though! Git并没有真正使用此文件!
  • In between these two copies, Git keeps the copy of the file that it will put into the next commit that you make. 在这两个副本之间,Git保留文件的副本,该副本将放入您下一次提交的文件中。 This copy is in what Git calls, variously, the index , or the staging area , or the cache . 该副本位于Git所谓的索引暂存区缓存中 You can view this copy with git show : path . 您可以使用git show : path查看此副本。

When you run git commit , Git packages up whatever is in the index right then , and freezes those contents to make the new commit snapshot. 当你运行git commit ,Git的封装了无论是在指数权的话 ,并冻结这些内容,以使新提交的快照。 This is why you must keep running git add so often. 这就是为什么您必须经常运行git add原因。 The git add command copies the contents of some work-tree file into the index, updating or even creating the index version. git add命令将某些工作树文件的内容复制索引中,以更新甚至创建索引版本。

It's the index version of the file that matters to Git. 对Git而言,这是文件的索引版本。 The committed versions are frozen into commits and cannot be changed. 提交的版本将冻结为提交,无法更改。 They last as long as the commit lasts. 只要提交持续,它们就会持续。 The work-tree version of each file is the one you can see and work with, but it's not the one that Git cares about. 每个文件的工作树版本都是您可以查看和使用的版本,但不是Git关心的版本。 The index version of each file is the one Git needs, in order to make new commits—and making new commits is the reason Git exists, so Git cares about commits and the index . 每个文件的索引版本是Git进行新提交所需要的文件-进行新提交是Git存在的原因,因此Git关心提交索引

The work-tree is for you. 工作树适合您。 Git doesn't want or need it for anything; Git不需要任何东西。 it's for you to work with, and in, and to copy out of to update the index, that Git needs for new commits. Git进行新提交需要与您一起使用和导入,并进行复制以更新索引。

Summary: commits are snapshots; 摘要:提交是快照; diff compares snapshots diff比较快照

All of this is how, and why, Git makes snapshots . 所有这一切都是Git制作快照的方式以及原因。 Git does not deal in changes. Git不处理任何更改。 It makes snapshots. 它制作快照。 You can, at any time, compare two snapshots to see what's different between them. 您可以随时比较两个快照,以查看它们之间的区别。 What you see will depend on which two snapshots you select. 您看到的内容取决于选择的两个快照。

You can also compare a snapshot (a commit) with a proposed snapshot (the index), or compare a snapshot to your work-tree, or compare your proposed snapshot (the index) to your work-tree. 您还可以将快照(提交)与建议的快照(索引)进行比较,或者将快照与工作树进行比较,或者将建议的快照(索引)与工作树进行比较。 The git diff-tree command only compares actual snapshots. git diff-tree命令仅比较实际快照。 To obtain the other comparisons, you need one of the other plumbing commands, or the porcelain git diff . 要获得其他比较,您需要使用其他水暖命令之一或瓷器git diff

The git status command also runs git diff for you. git status命令还会为您运行git diff When you use git status , it actually runs two such diffs. 当您使用git status ,它实际上运行两个这样的差异。 One compares the current snapshot, HEAD , to the index—to the proposed commit. 一个将当前快照HEAD与索引进行比较,并与提议的提交进行比较。 This is where changes staged for commit comes from. 这是为提交而进行的更改的来源。 The second comparison compares the index to the work-tree. 第二个比较将索引与工作树进行比较。 This is where changes not staged for commit comes from. 这是未进行提交的更改的来源。 Neither the index nor the work-tree have any actual changes; 索引和工作树都没有任何实际更改; the changes that git status or git diff show are the result of comparing actual snapshots (commits), proposed snapshots (the index), or potential snapshots (the work-tree). git statusgit diff显示的更改是比较实际快照(提交),建议快照(索引)或潜在快照(工作树)的结果。

You can use the below:- 您可以使用以下内容:

git diff --name-only $GIT_PREVIOUS_COMMIT $GIT_COMMIT

Or there is a function in jenkins changeSets which can be used. 或者有在詹金斯的功能变更可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM