How do you use Git with multiple computers?

Viewed 24307

I'm trying to use git and I have many computers. I usually work on my desktop, but sometimes use laptops.

How do you sync source code on git when you go home from work and you didn't finish working on the code? Do you send a patch file through email or just commit to git the incomplete codes?

I agree the latter is more convenient, but I think it makes the git changelog dirty.

5 Answers

You can also use the git reset command to "erase" all "unfinished" commits that you don't wish to appear in history.

Say you have in a branch master a commit_1. Then you commit from different computers save1, save2 and save3 to master, pushing and pulling your work each time you change your computer. Then with:

git reset --soft <commit_1_hash>

master (and HEAD) will point to commit_1. But the trick here is that your working tree and your index (staging area) still have the files from save3. You then immediately commit, for exemple:

git commit -m "commit_2"

Now all the saves are not going to appear on the linear history. Also by leaving a branch pointing to commit_1 you can just use the branch name instead of the hash value.

If you forget the --soft flag your index will be populated with the commit_1 files, but not your working tree, so you just have to add the files manually with add. DO NOT run reset with the --hard flag, as you will replace the working tree and lose any reference to save3. If you are afraid of that just create a new temporary branch pointing to save3, so if anything happens during the process you have a backup reference.

Also if you mess things up you can run git reflog and look for save3 in there, it should be there for a few months. More on the git reset command and the similarities and differences with git checkout can be found on Pro Git, specially on the chapter Reset Demystified.

Related