Danijel
Danijel

Reputation: 8620

Temporary saving current Git working tree state?

I am working in a (very important) public Git branch. I have a very dirty working tree: several files modified, some added, some staged, but none commited yet. I will not be ready to commit any time soon. I would like to somehow save current state to able to come back to it later if necessary - "just in case". Is there a recommended way to do this? Or my workflow is completely wrong?

Upvotes: 1

Views: 425

Answers (2)

Mr.Christer
Mr.Christer

Reputation: 802

My recommendation would be to create a topic branch. Work on that branch, commit often. Every commit should be a rather small easily reviewable change that makes sense. Do not create behemoths. Also push your branch often so other can work/review it and so that it present somewhere other than just locally (if disaster strikes). When you feel that your work is finished you should rebase (merge commits, edit commits) and cleanup your work. When it looks best you either merge your branch to the main branch or rebase your branch (rebasing will make it look like you made the commits on top of the main branch) to the main. After that you can delete your topic branch.

To create a topic branch

git checkout -b <topic-name>

To push your branch to origin

git push -u origin <topic-name>

Review the chapter about rebasing as it will make your work look professional: git-rebase

Learn rebasing so that it becomes second nature. It is a great tool once mastered.

Amending commits is typically done to correct something.

Stashing can work if you want to save small changes you made locally. If they become bigger it quickly becomes hard to overview what happened. They are much harder to administer over time.

I hope this helps.

Upvotes: 4

blue112
blue112

Reputation: 56582

Saving the current workspace

It's called a commit. A commit is local and you don't have to push it if you don't want to. Don't be afraid to create a new commit with a message "Work in progress".

git commit -a -m "WIP"

Then, when you make some more changes, just amend it

git commit --amend -a

It will save the new modification in the same commit.

If you want to transform that into a real commit, just modify the commit message

git commit --amend -m "New message"

Upvotes: 4

Related Questions