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

发布于 2024-11-07 11:23:31 字数 598 浏览 0 评论 0原文

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

所以我想问的是:

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

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

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

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).

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蘑菇王子 2024-11-14 11:23:31

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

  1. 假设 origin 在每种情况下都指 GitHub 存储库,您应该只运行 git fetch origin。然后“远程跟踪分支”origin/master 将位于 GitHub 上的 master 版本,您可以使用 git diff master origin/master 将其与您的 master 进行比较查看这两个版本之间的差异,或使用 git log master...origin/master 查看 masterorigin/master< 特有的提交/代码>。
  2. 是的,您可以将存储库复制到 USB 记忆棒并从那里提取。但是,如果您想从 USB 闪存盘拉到另一台计算机上,则需要设置一个引用 USB 闪存盘上的存储库位置的远程。

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

Answering your first two questions in turn:

  1. Assuming that origin refers to the GitHub repository in each case, you should just run git fetch origin. Then the "remote-tracking branch" origin/master will be at the version of master on GitHub and you can compare it with your master with git diff master origin/master to see the difference between those two versions, or git log master...origin/master to see the commits which are unique to either master or 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.

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.

空宴 2024-11-14 11:23:31

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

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

1) 通常当您克隆远程存储库时,您需要 -t 标志来跟踪您正在复制的远程分支:

git checkout -tb my_branch origin/my_branch

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

git branch --set-upstream my_branch origin/my_branch

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

对于您的第二个问题,是的,只要您在您的计算机上使用相同的用户。 Git 使用 SSH 身份验证,因此只要正确的用户运行命令,存储库位于何处并不重要,因此它会随请求发送正确的 SSH 凭据。

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

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

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

git checkout -tb my_branch origin/my_branch

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

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:

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.

吝吻 2024-11-14 11:23:31

对于其他人,他们来到这里是因为他们想在没有 git fetch 的情况下检查新提交(即不“污染”本地 git):

我发现的一个技巧是使用 git ls-远程并检查远程分支的提交哈希是否与本地分支匹配。

这是一个简单的 bash 脚本,它可以做到这一点:(

#!/bin/bash

# origin/branch we are interested in
origin="origin"
branch="master"
# last commit hash
commit=$(git log -n 1 --pretty=format:%H "$origin/$branch")

# url of the remote repo
url=$(git remote get-url "$origin")

for line in "$(git ls-remote -h $url)"; do
    fields=($(echo $line | tr -s ' ' ))
    test "${fields[1]}" == "refs/heads/$branch" || continue
    test "${fields[0]}" == "$commit" && echo "nothing new" \
        || echo "new commit(s) availble"
    exit
done

echo "no matching head found on remote"

我需要这个用于基于 git 的包管理器,它在检查更新时无法调用 git-fetch )。

For anyone else, who came here because they wanted to check for new commits without git fetch (i.e. without "polluting" your local git):

A trick that I found is to use git ls-remote and check if the commit hash of the remote branch matches your local one.

Here is a simple bash script that does that:

#!/bin/bash

# origin/branch we are interested in
origin="origin"
branch="master"
# last commit hash
commit=$(git log -n 1 --pretty=format:%H "$origin/$branch")

# url of the remote repo
url=$(git remote get-url "$origin")

for line in "$(git ls-remote -h $url)"; do
    fields=($(echo $line | tr -s ' ' ))
    test "${fields[1]}" == "refs/heads/$branch" || continue
    test "${fields[0]}" == "$commit" && echo "nothing new" \
        || echo "new commit(s) availble"
    exit
done

echo "no matching head found on remote"

(I needed this for a git-based package manager, that could not afford to call git-fetch when checking for updates).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文