What should we do when we change a machine?

1

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?

SoftTimur

Posted 2017-12-25T23:10:39.273

Reputation: 649

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 – 2017-12-26T04:10:53.517

Answers

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.

Patrick Mevzek

Posted 2017-12-25T23:10:39.273

Reputation: 1 334

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 – 2017-12-26T00:34:06.557

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 – Patrick Mevzek – 2017-12-26T00:36:50.587

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 – 2017-12-26T00:46:19.350

See my updated answer. – Patrick Mevzek – 2017-12-26T00:51:04.247

Thanks for this update. I did git reset --hard 19b3664, it gave me HEAD is now at 19b3664 m=back to no lazyload and no toModifyBeforeSubmit, but I notice any change in the github page of the project. Should I do a push or pull or something? – SoftTimur – 2017-12-26T00:59:42.003

1git 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. – Patrick Mevzek – 2017-12-26T01:05:40.257

I see, I used git reset --hard 19b3664 followed by git push -u origin master --force, and all the useless commits are erased in the github web page... – SoftTimur – 2017-12-26T01:12:28.170

But ask your users to synchronize themselves ASAP because if anyone continue working on the previous master branch (who has write access to your repository), when they will push all commits would reappear. And even for people doing PRs on top of your work they need to have the correct master branch – Patrick Mevzek – 2017-12-26T01:29:07.753