git bash:如何检查是否有新的提交可用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6006759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:00:17  来源:igfitidea点击:

git bash : how to check if there's a new commit available

gitbash

提问by Henson

I use git with my friend to collaborate on a project. He created a repo in github consisting folders of each of our names, so everytime i update something, I upload it to this folder then push it to github, and so does he. So everytime there's a new commit, we have to pull from github and copy paste the edited files to our own localhost (wamp).

我和我的朋友一起使用 git 来合作一个项目。他在 github 中创建了一个 repo,其中包含我们每个人的名字的文件夹,所以每次我更新一些东西时,我都会将它上传到这个文件夹,然后将它推送到 github,他也是。因此,每次有新提交时,我们都必须从 github 中提取并将编辑过的文件复制粘贴到我们自己的本地主机 (wamp)。

So what i want to ask :

所以我想问的是:

  1. How do I check if there's a new commit from git bash without checking github? Git pull will pull the latest version i know, but what if i just want to check first?

  2. Say my git folder is located in D://git/project_name. Can I copy this folder to another location (for example USB Flash Disk) then do a git pull from this USB?

  3. If our method isn't effective, do you have any proposal on how we should use git? We're using CodeIgniter framework, so at first I proposed to just git push the whole CodeIgniter folders, but my friend said it's dangerous (cos the whole config and database is there) and unnecessary (since the main system folder will most likely be untouched). So we're using this current method (having two folders with our names and pushing and pulling everytime there's an update.

  1. 如何在不检查 github 的情况下检查 git bash 是否有新提交?Git pull 会拉取我知道的最新版本,但如果我只想先检查怎么办?

  2. 假设我的 git 文件夹位于 D://git/project_name。我可以将此文件夹复制到另一个位置(例如 USB 闪存盘),然后从此 USB 执行 git pull 操作吗?

  3. 如果我们的方法无效,您对我们应该如何使用git有什么建议吗?我们使用的是 CodeIgniter 框架,所以一开始我建议只 git push 整个 CodeIgniter 文件夹,但我朋友说这很危险(因为整个配置和数据库都在那里)而且没有必要(因为主系统文件夹很可能不会被触及)。所以我们正在使用这个当前的方法(有两个带有我们名字的文件夹,并且每次有更新时推拉。

回答by Mark Longair

Answering your first two questions in turn:

依次回答你的前两个问题:

  1. Assuming that originrefers to the GitHub repository in each case, you should just run git fetch origin. Then the "remote-tracking branch" origin/masterwill be at the version of master on GitHub and you can compare it with your master with git diff master origin/masterto see the difference between those two versions, or git log master...origin/masterto see the commits which are unique to either masteror origin/master.
  2. Yes, you can copy the repository to a USB stick and pull from there. However, if you want to pull from the USB key onto another computer, you'll want to set up a remote that refers to the location of the repository on the USB key.
  1. 假设origin在每种情况下都指的是 GitHub 存储库,您应该只运行git fetch origin. 然后“远程跟踪分支”origin/master将位于 GitHub 上的 master 版本,您可以将其与您的 master 进行比较,git diff master origin/master以查看这两个版本之间的差异,或者git log master...origin/master查看masterorigin/master.
  2. 是的,您可以将存储库复制到 U 盘并从那里提取。但是,如果您想从 USB 密钥拉到另一台计算机上,您需要设置一个远程,以引用 USB 密钥上存储库的位置。

With regard to whether there is a better way to do what you're doing, I'm afraid I don't understand clearly enough what you're doing or trying to achieve to comment sensibly. However, having folders in the repository named after each developer with (presumably) very similar source code in them sounds a little suspect - you would typically use branches to deal with divergent versions of the same code.

关于是否有更好的方法来做你正在做的事情,恐怕我不太清楚你在做什么或试图实现什么,以便明智地发表评论。但是,存储库中的文件夹以每个开发人员的名字命名,其中包含(大概)非常相似的源代码听起来有点可疑 - 您通常会使用分支来处理相同代码的不同版本。

回答by Mauvis Ledford

If your branch is properly tracking the remote branch, you should be able to do a git statusand see if you're ahead or behind the remote and by how many commits.

如果您的分支正确跟踪远程分支,您应该能够执行 agit status并查看您是领先还是落后于远程分支以及提交了多少次。

This was a little confusing about git for me. Here's the answer in three parts:

对我来说,这对 git 有点困惑。以下是三个部分的答案:

1) Usually when you clone a remote repo, you need the -tflag to track the remote branch you're copying:

1) 通常当你克隆一个远程仓库时,你需要这个-t标志来跟踪你正在复制的远程分支:

git checkout -tb my_branch origin/my_branch

2) If you forget to do this, you can always do this later by doing:

2) 如果您忘记这样做,您可以稍后通过执行以下操作来进行:

git branch --set-upstream my_branch origin/my_branch

3) If your remote branches are the same name as your local branchesyou should be able to just set git to track all remote branches automatically:

3) 如果你的远程分支与你的本地分支同名,你应该能够设置 git 来自动跟踪所有远程分支:

git config push.default matching             <-- for this repo only
git config --global push.default matching    <-- for all git repos that this user deals with on this machine

For your second question, yes that should be fine as long as you're using the same user on your machine. Git uses SSH authentication so it doesn't matter where the repo is located as long as the right user is running the command so it sends the proper SSH credentials along with the request.

对于你的第二个问题,是的,只要你在你的机器上使用同一个用户就可以了。Git 使用 SSH 身份验证,因此只要正确的用户正在运行命令,repo 所在的位置并不重要,因此它会随请求一起发送正确的 SSH 凭据。