12

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.

Henson
  • 5,563
  • 12
  • 46
  • 60
  • 2
    possible duplicate of [git: check if pull needed](http://stackoverflow.com/questions/3258243/git-check-if-pull-needed) – Karl Bielefeldt May 15 '11 at 06:00
  • for check new commits in remotes I use this [git-prompt](https://github.com/juanpabloaj/git-prompt), this [lines make the job](https://github.com/juanpabloaj/git-prompt/blob/master/git-prompt.sh#L522-550) – JuanPablo Jul 20 '14 at 04:51

3 Answers3

14

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.

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
5

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.

Mauvis Ledford
  • 40,827
  • 17
  • 81
  • 86
  • we're new at git so it's kinda confusing for us too :) So basically, my friend created a repo at github and invited me as collaborator, so both of us can push to the repo branch master (default?) So we're not really using branch or anything. Is it bad? So basically everytime i wanan know if there's a new commit, i have to go to github and check, otherwise i can just pull all the latest without knowing what the new files updated (imagine pulling after vacation). Is there anyway to check what files are new? what about the third question? do you have any suggestion? – Henson May 15 '11 at 06:35
  • You are always on a branch, in this case master branch. Do step #2 above replace "my_branch" with master. Then have your partner commit a file to github and then do a git status on your machine to see if it knows you are one commit behind. – Mauvis Ledford May 15 '11 at 06:46
  • Regarding your setup, consider creating symbolic links. ln -s my/git_project /Applications/MAMP/htdocs/my_project if you're on a Mac. Not sure about Windows. – Mauvis Ledford May 15 '11 at 06:48
  • @Mauvis Ledford: "matching" is the default value for `push.default`, so you don't need to configure that. In fact, if you've set up an association with an upstream branch, as you suggest in your 1) and 2), it's [much less confusing to set push.default to "tracking"](http://longair.net/blog/2011/02/27/an-asymmetry-between-git-pull-and-git-push/). In addition, I suspect that the questioner and the collaborator have cloned from GitHub, or followed their instructions for creating a new repository, so steps 1) and 2) should be unnecessary. – Mark Longair May 15 '11 at 08:36
  • @Mark, the poster only asked to tell if there was new commits and not see a diff of the changes. If he had set things up to track correctly, wouldn't a simple "git status" tell the poster how far ahead or behind in commits he is? – Mauvis Ledford May 15 '11 at 20:16
  • @Mauvis: the information on the second line of the output from `git status` may not be accurate if the OP hasn't run `git fetch origin` beforehand, and that information is easy to miss in any case. Also it's nice to see the commit messages with `git log master...origin/master`, I think. – Mark Longair May 15 '11 at 20:24
0

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

vgratian
  • 45
  • 9