1

I have always been developing a big project in one machine. I use github to do version control. Now, I just got a new machine. I want to develop the project on the new machine. I am wondering what's the best practice.

What I tried is, in the new machine:

git clone https://github.com/softtimur/project.git

In the folder project, i did

git checkout master

which returns

Already on 'master'
Your branch is up-to-date with 'origin/master'.

Then, I wanted to go to a previous version of the files:

git checkout 19b3644 

And I got an message:

Note: checking out '19b3664'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 19b3664... m=back to no lazyload and no toModifyBeforeSubmit

My question is not about how to fix detached Head state. I want to know whether I should create a new branch in this new machine. I don't need to code from 2 machines, I could make the new machine master, and I don't need to touch the old machine anymore.

I always have trouble in understanding git... Could anyone help?

1
  • Is the title "What should we do when we change a machine?" not lacking, or even misleading, since the question is really "how do I use git?"
    – Xen2050
    Commented Dec 26, 2017 at 4:10

1 Answer 1

3

If you are switching definitely from one machine to the other, just do your git clone and resume work from there at whatever tag/branch you want. You do not necessarily need a new branch, or at least this is unrelated to the fact that you changed your machines, branches are just to organize your work so this all depends on how you use git.

git is a decentralized VCS: you have as many master branches as you have repositories, branches are per repository, and then you can just "link" them from one repository to the other.

Warning however: when you clone a git repository to start working elsewhere you may not retrieve everything because some local stuff is not stored inside the repository (and hence not distributed) so you will need to make extra care to move it yourself if your need it. In that area you have at least to consider: dirty files in worktree, the stash, your config, your hooks, etc.

Update based on your following comments: Your problem does not seem then to have anything to do with changing servers. If you want to forget (that is completely forgetting these commits and not being able to retrieve them in the future) some commits and go back to 19b3664 just do git reset --hard 19b3664. Your master branch HEAD would now again be at this commit. Again: you are loosing all previous commits that were after this one, so be extremely careful (or have good backups). I would recommend instead the following:

  • git checkout master
  • git branch previous_work
  • git reset --hard 19b3664

The end result would be the branch master back at this specific commit id but then you will always have branch previous_work (use any name that makes sense in your case) around so that you can come back to it if needed.

8
  • I don't want to create new branch either... But is it normal that it told me You are in 'detached HEAD' state. after checkout? Actually I want to go to a previous version, should I use checkout?
    – SoftTimur
    Commented Dec 26, 2017 at 0:34
  • it just means that this commit id has no tag nor branch HEAD on it. Nothing to worry specifically about and the message tells you how to correct that. But why you need this commit specifically? Why can't you continue your work from master's HEAD commit? Yes, if you want previous "version" you can use checkout but create a branch out of it, excatly as given in message, with -b branchname Commented Dec 26, 2017 at 0:36
  • Because I wrongly did a massive change in my previous commit, and I want to ignore it by reverting to a specific version before that. OK, it seems that I have to create a new branch for this. But after git checkout -b newbranch, what should I do? I want to finally come back to master branch in this new machine.
    – SoftTimur
    Commented Dec 26, 2017 at 0:46
  • See my updated answer. Commented Dec 26, 2017 at 0:51
  • 1
    git is decentralized VCS, things you do locally do not appear elsewhere until you do a push but for your case you will probably need a --force as things like that are not expected to happen (see github documentation they may explain this specific case). You could instead of the previous solution, just do the clone, then use revert to create new commits that undo all commits after the one you are interested in, and then push. But things all depend on how you use branches, who are your users, etc... This is becoming too specific. Commented Dec 26, 2017 at 1:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .