Git - If we want to take a snapshot - should we Branch our should we Commit? - git-commit

Let's say I'm working on this thing, and I want to preserve this state, so that, if something goes wrong, I can revert to it.
According to this, it's not clear to me:
What should we do when on this situation:
commit or create branch ?

I think Git Tag is what you are looking for.
http://git-scm.com/book/en/v2/Git-Basics-Tagging

Related

Is there a way to add an alternative fix in Gerrit?

Right now, I am committing the changes for a bug fix (ticket), and then do a
git push origin HEAD:refs/for/master
and then go through the code review on Gerrit. If a coworker proposed a totally different fix, is there a way to try this alternative fix somehow as the same Gerrit code review but as an "alternative way"?
I thought of using this way:
do a git log to look at the commit number exactly before the commit
git checkout that commit
git checkout -b ticket-1234-alternative to start another branch and try the alternative fix
So this method starts from how the project was when you make the fix, and then lets you do an alternative fix, as a separate branch. Is there a way that might not involve creating a different branch but using the same branch and possibly associate it with the same Gerrit code review?
If you want to try a different fix you can choose one of the following:
1) You can amend your original commit (git commit --amend) and push the new commit as the patchset2 to the same change. If you decide to go back to the original commit you'll need to push it again as a patchset3.
2) You can make a new commit based in the parent commit (as you have suggested) and push to a new change on Gerrit. When you decide which change will be used you just need to abandon the other one.

How do I load the second to last committed version of my project using git?

Suppose that I made some changes and did this:
git add -A
git commit -m "comment commit_1"
Now I made more changes and did this again:
git add -A
git commit -m "comment commit_2"
Now, I basically want to discard commit_2 and start modifying my project again at the point of commit_1.
How do I do this?
If you don't mind losing the revisions at all (as if they never happened at all), you can do:
git reset --hard HEAD~
If you want to keep the current history and get a new version on top of what you have where you get rid of the changes of the revision, you can do:
git checkout HEAD~
git reset --soft the-branch
git commit -m "Taking back changes from the last revision"
# And if you like the result of this:
git branch -f the-branch
git checkout the-branch
There you go.
Now, I basically want to discard commit_2 and start modifying my project again at the point of commit_1.
If you literally want to discard commit_2, then it may be that you can use
git reset --hard HEAD^
(Note that at the time I wrote this, another post incorrectly told you to use HEAD~2; that would discard both commits, which is not what was asked.)
There are caveats to using commands that remove commits from a branch.
The first is that you could lose the changes you made in affected commits. That may seem a strange warning - you asked how to discard the changes - but realizing that mistakes happen, it's cause to be careful with these commands. (Just to clarify - if you did something like this accidentally, the changes would not be immediately lost forever; the reflogs offer a little safety net.)
Other issues arise if your repo has remotes and you've pushed the commit(s) in question to any of the remotes. It doesn't sound like that's an issue here, but if it is then there is more you should consider before removing commits from a branch.
But if you've looked it over and determined that you really do just want to discard the commits, the above should work. Do note, HEAD^ is just one of the possible expressions that refers to the second-to-last commit. At the moment it may be the simplest thing to use, but it won't always refer to that particular commit. HEAD^ really means "the commit which is the parent of the currently-checked-out commit".
If you're on the master branch, then master^ would also work - and would not depend on what's currently checked out. Even that changes whenever the master branch moves; if for some reason you need an expression that would continue to refer to the particular commit, you could create a tag
git tag mytag master^
and then mytag would work; or you could look up the commit ID of the commit in question (though it won't be a user-friendly name)
But I digress...
This is different from - and more drastic than - merely loading the 2nd-to-last commit as the question title suggests. To simply update your working tree with the 2nd-to-last commit's state, you could
git checkout HEAD^
This moves the HEAD (i.e. changes what's checked out) and updates the index and work tree, but it still keeps the commit on master in case you need it again later. However, it also puts you in a state called "detached head", meaning that git isn't really expecting you to create new commits from here.
If you want to both keep the old commit around, and be in a state where you can add commits on top of commit_1, then you need to create a new branch. If you did the previous checkout command, you could then
git checkout -b mybranch
and new changes could be committed to the new branch while leaving the original commit_2 on master. Or, you could put a branch (or tag) on commit_2 and then move master back to commit_1.
git checkout master
git branch mybranch
git reset --hard HEAD^
That may seem like a confusing number of options; but really that's because each one serves a somewhat different purpose, and only someone involved in your project is likely to have all the context to decide which makes sense.
So tl;dr - it sounds like you might be looking for git reset, but just doing that alone is only suitable if you really intend to permanently discard the changes from commit_2. If you really would want to preserve them for future reference (But maybe just weren't sure if that's an option) then there are ways to do that, too.

How to add descption to a given patchset in Gerrit?

I can create new patchset to a given Gerrit without issues.
However, I would like to describe what the patchset is about. I have been doing that as part of the commit message, as I commit -amend and push the new patchset with the revised comment.
Is there a well defined way of describing the patchset in the commit message, so Gerrit can automatically set its description? The only mentioning of the description I found so far was from this page:
https://gerrit-review.googlesource.com/Documentation/concept-patch-sets.html#_description
Heh, looks like I found a potential answer for my own question. :)
If I encode the push command with a %m= I seem to be able to set the description for the patchset. It feels a little hacky, in that I need to replace
spaces with _ and percent encode characters as well.
That is described gerrit's documentation for code review.
Example:
git push ssh://john.doe#git.example.com:29418/kernel/common HEAD:refs/for/experimental%m=This_is_a_rebase_on_master%21
Any other/better way that you may know about?

How to get previous patch in gerrit

I am Sajid, I like to know Is it possible that I can go back to my previous patch in my commit. I can explain:
I commit something. On the way to delivery, i fixed and update things and pushed into gerrit with commit --amend. But what I am experiencing now my last patch is a faulty so I like to go back to previous patch. and I don't know how can i point to my previous patch. And I also like to know will it be the same procedure even If I go to any previous patch.
thanks in advance.
As this answer said, patchsets can not be reverted. so first you have to checkout one of the previous patchset. you can get the command from Gerrit review board at each patchset download panel. For example:
git fetch https://android.googlesource.com/kernel/tegra refs/changes/43/69243/2 && git checkout FETCH_HEAD
After checking it out amend it to generate new commit hash then pushed again as a new patchset.

Not push Sourcetree/Bitbucket

I am new to Bitbucket/Sourcetree. After clone the repository to my local machine. I made one one comment to test it. Say it was:
just add commit
Then I clicked commit button. After that I didn't want to push it as it just was an practice. I prehaps clicked Discard(not recall clearly). Now it shows Revert "just test commit"
My question is that I really don't want to push it, how to get rid of it as the "Push" button shows "2" on it. See the image.
I tried the command git stash but it was not working.
It is tracking history. So if you pushed the 2 "changes", it would just show that you made a change, and then reverted it back.
There is an option in Git called "stash" which basically puts aside the local changes. You can do that, then just delete the stash. In SourceTree you can find the stash option under the "Repository" menu (at least on OSX).
If all you want to do is "reset" your local repo/branch to match the remote, this thread is a really good discussion about how to do that: Various ways to remove local Git changes

Resources