Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git equivalent to TFS Shelve before trying something risky

In TFS, I used to shelve my changes as a back-up before trying something risky or experimenting with something.

If everything went okay, I just deleted the shelve. Otherwise, I unshelved the back-up and continued.

I wasn't interested in sharing those changes with anybody.

What would be the equivalent operation in Git? I'm using VS2015 with GitHub extension, by the way.

like image 835
user11081980 Avatar asked Sep 02 '25 16:09

user11081980


2 Answers

Create a branch. Let's say you're working on master, and you decide you want to experiment with a solution. Create a new branch:

git checkout -b my-spiffy-new-feature

Now, continue your work on the my-spiffy-new-feature branch. What if suddenly you have a brilliant idea for a completely different solution? Just create a new branch from master, either by running:

git checkout -b another-awesome-feature master

Or equivalently:

git checkout master
git checkout -b another-awesome-feature

In either case, do all the work on your branch. If you decide you like the feature, merge it back into the master branch:

git checkout master
git merge my-spiffy-new-feature

You can let the branches hang around, or you can delete them if they are no longer of interest:

git branch -d my-spiffy-new-feature

Note that the above will fail if you try to delete a branch that you haven't merged into it's upstream branch (that is, the branch from which it originated); if you really want to delete a branch, even though you haven't merged it:

git branch -D another-awesome-feature
like image 104
larsks Avatar answered Sep 04 '25 05:09

larsks


The closest thing to TFVC - Shelvesets in git I feel is stash. As others have already mentioned that it has it's own disadvantages, one being that it is not branch specific. But still it is a good alternative depending on your requirement.

In Visual Studio 2019 - if you are looking for a quick way not to commit your changes to the current branch yet, and want to save your change for later, you can use stash like this.

enter image description here

In Team Explorer, you have two options for stash:

  1. Stash All - creates a stash that contains all the uncommitted changes, and it also undoes all the uncommitted changes in your branch
  2. Stash All and Keep Staged (--keep-index) - creates a stash that contains all the uncommitted changes and also keep the changes in the branch that you have already staged for a commit

enter image description here

To get your stash back, you can right click on your stash under Stashes section. It will give you some options:

  1. View Changes - shows all the changes in the stash
  2. Apply - applies the changes to the branch and it will keep the stash
  3. Pop - Apply the stash and finally Drop it (see Drop below)
  4. Drop - deletes the stash

Apply and Pop options have two sub-options:

  1. Apply/Pop and Restore Staged - applies/pops the changes to your branch and ensures that files that were staged before are staged again
  2. Apply/Pop All as Unstaged – applies/pops the changes to your branch and it won’t stage the files that you had staged before.

Credit: Thomas Claudius Huber's article. Link again.

like image 27
Sri Reddy Avatar answered Sep 04 '25 07:09

Sri Reddy