Skip to contentSkip to conversation

simple git use case

DISCUSSION Erik Iverson 3 participants 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

Dear all,

I sincerely hope this is not an annoying question, I promise I have tried to do my homework here, but am stuck. My use case is simple. I have a desktop and a laptop. When I go on the trips, I'd love to be able to bring my "git-test" directory with me on the laptop, code some, and then get the new revisions back on the desktop when I get home (bonus if I can get the revisions back to my desktop over the internet while still on the road, in case, for example, my laptop gets stolen). No one else will be working on this stuff, it's strictly for me.

OK, so here's what I do.

Desktop (dt):

dt> cd git-test
dt> git-init
Initialized empty Git repository in /home/erik/projects/git-test/.git/ dt> git add .
dt> git-commit -am 'initial commit'
Created initial commit c150815: initial commit

 3 files changed, 14 insertions(+), 0 deletions(-)
 create mode 100644 test.R
 create mode 100644 test.sas
 create mode 100644 test.tex

Looks good...
Now over to the laptop (lt)!

lt> git-clone ssh://myip/path/to/project

And great, I have the three test.* files, looks good!
So I make some changes to test.R on laptop, like I'm on the road. Then, on laptop:

lt> git-commit -am 'an update'

Looks good.

Now I become a mouth-breather...and need some help :).

My instinct was to git-push from the laptop,

lt> git-push

which succeeds, but then a git-pull
from desktop says:

dt> git-pull
fatal: 'origin': unable to chdir or not a git archive
fatal: The remote end hung up unexpectedly

OK, so look around mailing list archives...I try, without knowing what it means at all:

dt> git-config branch.master.remote .
dt> git-pull
From .
* branch HEAD -> FETCH_HEAD
Already up-to-date.

Hmmm, so it did something, but not what I'd expect. How about:

dt> git-checkout
M test.R

So it realizes that file has been modified...

dt> git-checkout -f

This gives no message of any type, and now I see my modified test.R file on my desktop, so good! I'm happy with this, but I must know, it this the Right Way to use git for my use case, or am I doing something silly?

Thank you for providing this software.

Best Regards,
Erik Iverson

#1 lore raw
Quoted 10 lines · Erik Iverson

Dear all,

I sincerely hope this is not an annoying question, I promise I have tried to do my homework here, but am stuck. My use case is simple. I have a desktop and a laptop. When I go on the trips, I'd love to be able to bring my "git-test" directory with me on the laptop, code some, and then get the new revisions back on the desktop when I get home (bonus if I can get the revisions back to my desktop over the internet while still on the road, in case, for example, my laptop gets stolen). No one else will be working on this stuff, it's strictly for me.

You may wish to read:

http://git.or.cz/gitwiki/GitFaq#head-b96f48bc9c925074be9f95c0fce69bcece5f6e73
(Why won't I see changes...)

and:

http://git.or.cz/gitwiki/GitFaq#push-is-reverse-of-fetch
(How would I use "git push"...)

Quoted 28 lines

OK, so here's what I do.

Desktop (dt):

dt> cd git-test
dt> git-init
Initialized empty Git repository in /home/erik/projects/git-test/.git/ dt> git add .
dt> git-commit -am 'initial commit'
Created initial commit c150815: initial commit

 3 files changed, 14 insertions(+), 0 deletions(-)
 create mode 100644 test.R
 create mode 100644 test.sas
 create mode 100644 test.tex

Looks good...
Now over to the laptop (lt)!

lt> git-clone ssh://myip/path/to/project

And great, I have the three test.* files, looks good!
So I make some changes to test.R on laptop, like I'm on the road. Then, on laptop:

lt> git-commit -am 'an update'

Looks good.

Quoted 7 lines

Now I become a mouth-breather...and need some help :).

My instinct was to git-push from the laptop,

lt> git-push

which succeeds

When you did the clone operation, you set things up such that the branch called "master" on the desktop is replicated to a branch called "origin/master" on the laptop. Whenever you do a "pull" on the laptop (which clone did for you initially), it will do two things:

1) Fetch the latest master from desktop and store it as origin/master on the laptop.
2) Merge origin/master (on the laptop) with master (again on the laptop) and update your working copy with the result.

Now, whenever you do a "push" on the laptop, it is *not* a symmetrical operation to fetch/merge. Rather, the push updates master on the desktop to match master from the laptop. However, the working copy on the desktop is not touched. So when you login to the desktop, you need to manually refresh the working copy from what was pushed. You do this with "git reset --hard master". But be careful, if you have made changes on the desktop and not committed them, these changes will be lost when you do the reset.

The FAQ entries above discuss two alternatives to make this safer. One alternative updates the working copy automatically and mostly safely via a hook, while the other alternative discusses making the "push" from the laptop symmetrical to the "fetch".

I'll elaborate on the later alternative slightly. You could do your clone like this:

lt> git-clone -o desktop ssh://myip/path/to/project lt> git branch -a
* master
desktop/HEAD
desktop/master

The only difference is we've used "-o" so that we have a more descriptive name for the desktop than "origin".

To make the push operation on the laptop symmetrical to the fetch (remember, pull = fetch + merge), do this:

lt> git config remote.desktop.push "+refs/heads/*:refs/remotes/laptop/*"

Now take a look at .git/config and notice the fetch and push are symmetric:

[remote "desktop"]
url = ssh://myip/path/to/project fetch = +refs/heads/*:refs/remotes/desktop/* push = +refs/heads/*:refs/remotes/laptop/*

Let's do a commit and push it:

lt> echo bar > bar && git add bar && git commit -m "added bar" lt> git push
To ssh://myip/path/to/project
* [new branch] master -> laptop/master

Back on the desktop:

dt> git branch -a
* master
laptop/master

Ah hah. So the changes have been pushed to laptop/master, but of course, the working copy is not up-to-date. When can fix that with:

dt> git merge laptop/master
Updating 785a74b..09ab37d
Fast forward

 bar |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 bar

This merges laptop/master into master, then updates the working copy with the result. If you'd like "git pull" to be an alias for "git merge laptop/master", you can:

dt> git config branch.master.remote .
dt> git config branch.master.merge refs/remotes/laptop/master dt> git pull
From .
* remote branch laptop/master -> FETCH_HEAD
Already up-to-date.

j.

#2 lore raw
Quoted 8 lines · Jay Soffian
Quoted 6 lines · Erik Iverson

and a laptop. When I go on the trips, I'd love to be able to bring my "git-test" directory with me on the laptop, code some, and then get the new revisions back on the desktop when I get home (bonus if I can get the revisions back to my desktop over the internet while still on the road, in case, for example, my laptop gets stolen). No one else will be working on this stuff, it's strictly for me.

Erik: I have the exact same situation, and I simply use a bare repository on the same desktop as a go-between. I think this is a better way of doing things -- gives you a lot of flexibility for all sorts of future situations (someone else joining the project, you having to temporarily use a *third* machine, etc).

Quoted 8 lines

Now, whenever you do a "push" on the laptop, it is *not* a symmetrical operation to fetch/merge. Rather, the push updates master on the desktop to match master from the laptop. However, the working copy on the desktop is not touched. So when you login to the desktop, you need to manually refresh the working copy from what was pushed. You do this with "git reset --hard master". But be careful, if you have made changes on the desktop and not committed them, these changes will be lost when you do the reset.

If you have a couple of minutes you may also want to take a look at
http://sitaramc.github.com/concepts-and-tips/0-terminology.html#a5
, where I have explained *why* all this is necessary. It doesn't (but probably should) talk about how to set up a bare repo etc...

[Git experts: be kind :-) The site itself is a work in progress, as is my knowledge of git. However, that particular page was written after that explanation and demo actually helped someone]