-1

For a lot of us programmers, we're constantly on the move. When im home I prefer to code on a PC, when im out however I need to code on a laptop. My question is how do some of you go between the two consistently and keep your code up to date between the systems?

At first I was just doing commits of unfinished work on my PC before going out of the house that way I could just pull and continue work on my PC. However, this leads to random commit messages saying, "switching to laptop", and such

I also tried using git stash but that obviously became hard to manage.

What do you use for being able to manage this without the annoying commit messages while having erasability? Doesn't have to be through GitHub either.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 1
    I've been there ... got a faster laptop, put most things on the laptop, but will probably be back there again next year, and the best answer I have is "make lots of little commits, then combine them into sensible commits later". – torek Oct 18 '22 at 00:38
  • 1
    Yah, collapsing a series of wip commits onto the base you want is as easy as `git reset --soft $base`, then your next commit has `$base` as its parent. Git's *built* to treat first-draft history as a big whole-checkout undo buffer. – jthill Oct 18 '22 at 02:34
  • Not really a _git_ answer, but Windowns Remote Desktop is amazing.. My laptop is my primary development machine, and from my desktop, I remote into the laptop. Different solutions for different people :) – Matt Clark Oct 18 '22 at 02:46

1 Answers1

0

Commit and push your unfinished work, like you're doing. A message of "wip" (Work In Progress) is sufficient, but write more if you need to remind yourself what you were doing.

When you get to the other computer pull the changes, like you're doing, and continue your work.

# git pull
origin
A - B - WIP [main]

local
A - B - WIP [main]

When you're ready to commit, instead of making a new commit, amend the existing commit. Use git commit --amend. This will add your changes to the last commit and give you an opportunity to rewrite the commit message.

git commit --amend actually replaces the wip commit with a new commit combining the wip commit with your edits. This is an important detail.

# edit edit edit
# git commit -a --amend
origin
A - B - WIP [main]

local
A - B - C [main]

Here commit C combines WIP with your new edits. You local branch has diverged from origin.

This is important because pushes can only add to history, a push can't change history. If you try to push it will be rejected because you'd overwrite the WIP commit. To overwrite a branch with your local one you need to force the push using git push --force-with-lease (don't use --force). I like to alias that as git repush.

# git push --force-with-lease
origin
A - B - C [main]

local
A - B - C [main]

This WIP/amend approach works well even if you just need to interrupt your work to change branches. It's more robust than stashing.


An alternative to the WIP/amend approach is WIP/redo. Do all the same stuff, but after you pull run git reset --soft HEAD~. This will undo the last commit, but leave all the changes in your staging area. Now you can continue to work on your unfinished edit as normal, commit as normal, and then force the push.

I like to alias git reset --soft HEAD~ as git redo.

Going back to our example above, here we are just after pulling the WIP.

origin
A - B - WIP [main]

local
A - B - WIP [main]

Then we git reset --soft HEAD~ to undo the WIP but keep the changes staged.

# git reset --soft HEAD~
origin
A - B - WIP [main]

local
A - B [main]

And then finish your edits and commit as normal.

# edit edit edit
# git commit -a
origin
A - B - WIP [main]

local
A - B - C [main]

The advantage of WIP/redo vs WIP/amend is with WIP/redo you can see and diff the complete change like normal. Either approach is fine, the end result is the same, it's a matter of taste.


Amending is fine if it's just the latest commit you want to rewrite. More general editing of the history can be done with an interactive rebase where you can rewrite commits, change commit messages, squash commits together, and so on. In your case, your "switching to laptop" commits would be squashed as a "fixup".

See Rewriting History for more information on amending and rebasing.

Schwern
  • 153,029
  • 25
  • 195
  • 336