开发者

How do you use Git with multiple computers?

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 th开发者_如何学编程e git changelog dirty.


Commit your work in progress and push it to a remote branch. When you're ready to merge it into your main branch you can use an interactive rebase to rewrite your history and eliminate "work in progress" commits.


Git has a concept of a "remote", such as a central server to push all of your code. You would push all of your changes to the remote (say from work), then when you get home, you can pull the commits you made from work.

GitHub is an excellent hosted Git solution that saves you the pain of having to set it up yourself. You can learn more about remotes, pushing, and pulling from ProGit.

As far as "incomplete" work, you could use a branch and push your branch to the remote. Branches allow you to create a "branch" of code off of the master (sometimes called a trunk). When you are done, you can merge all of the changes back to master. You can read more about branching here


Well first of all you probably want to make sure your employer is okay with this... they might not like it if you're taking company IP home with you. :-)

The answers about having a dedicated server like GitHub are good, but realize you're limited to either open source (which I doubt your employer would like) or having to pay for a small number of private repositories ($7 for 5 private repos, currently).

IMO the simplest thing to keep a copy of your git repo on your phone or a USB stick, adding the main repo as a remote. Sync this repo with your main one before you leave to go home. You don't need to set up a server at all; git works happily at the filesystem level.


What you really need is a single server side repository that is available publicly. You can use http://github.com/ for the same. Github allows you to create public repositories for free, creating private one's is a paid feature. If you wish to setup your own repository server, you can have a look at https://github.com/sitaramc/gitolite it's reasonably easy to setup.

Once you have a public repository then you can push from your desktop at work and then when home on your laptop, you can then pull to get the latest.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜