tisdag 30 maj 2017

Delete batches of tags in git

List tags

To list all tags, run
git tag

To list tags matching a pattern, use
git tag -l name_pattern 

For example, to list all tags with a name that begins with Release
git tag -l Release*

Delete tags locally

To delete a single tag
git tag -d name_of_tag

To delete multiple tags
git tag -d name_of_tag1 name_of_tag2

To delete all tags locally
git tag | xargs git tag -d  
(xargs takes the output from the first command and passes it as argument to the next)

To delete all tags that matches a name pattern
git tag -l Release* | xargs git tag -d

Delete tags on the remote

Assuming the remote is named origin and you want to delete all release tags for the 1.x and 2.x releases
git tag -l {Release/1.*,Release/2.*} | xargs git push --delete origin

REMEMBER to make sure that your pattern ONLY matches the tags that you want to delete. Inspect the result from the listing of matched tags before piping them to the next command.

Screenshot tool: Lightshot

Lightshot is a program that you can install for free in Windows (or Mac).

When installed, just press the Print Screen button and then select the area on the screen that you want captured.

When an area has been selected, it is possible to add text, arrows, boxes or freehand forms. When done, it's possible to copy the result to clipboard, save it to disk, print it or search for similar images on Google.

A really handy tool!

Download here

Revert a merge commit using SourceTree

Update 1 (no need to investigate the parents)

The use case I need this for is when a feature branch has been merged to master, and after that something arises that makes us want to remove that feature from the master branch again. In the text below I describe how to find the parents of a merge commit, but since the checked out branch always will become the parent number one when merging in another branch, the investigation of the parents isn't needed. The only command needed is therefore
git revert the_hash_of_the_commit_to_revert -m 1

Update 2 (commands for the commit message editor)

When the commit message editor opens up, just accept and exit by writing 
Or, if you want to change the commit message, press i, write your message and then
<Esc> :wq <Enter>

Revert a "normal" commit

Normally when you want to undo the changes of a commit that has been pushed, you can do a reverse commit inside SourceTree by doing a right click on the commit and choose "Reverse commit..." like in the image below.

But if the commit you're trying to reverse is a merge commit you'll get the message:

git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=manager-st revert --no-edit 5a1f18d0d344e4ce9f23b02752cf4674c973cdf4
error: Commit 5a1f18d0d344e4ce9f23b02752cf4674c973cdf4 is a merge but no -m option was given.
fatal: revert failed

Completed with errors, see above.

Which means you've got to use the Terminal window.

Revert a merge commit

The case

Suppose we have the state as seen in the image below. Suddenly we realize that the changes made in the demo_branch are wrong and have to be removed from the master branch.

To do this we have to issue the revert command and specify the -m option. The -m option specifies which of the parents that was the mainline that we want the resulting code to be like.

Identify the commit to revert

The commit we want to undo is the commit with id 0c9c102

Find the correct parent

To the right in the button bar in the top of SourceTree window there is a button for opening the Terminal window.

Click on it and you'll see the Terminal window below.

Run the command git log to list the commits. Find the commit we want to revert (when you´ve found your commit, stop the listing with ctrl-z). In the image below we see that commit 0c9c102 is a merge, having two parents with the ids 618383a (parent 1) and b8a9ad7 (parent 2).

In this case we want to use the commit in the master branch as parent, the commit with id 618383a that is. That parent was listed in the log as the first parent, therefore we refer to it as parent 1.

Execute the revert command

To recap, we want to revert the commit 0c9c102 using parent 1 as the index to the -m (mainline) option. Therefore the command to write in the terminal window becomes:

git revert 0c9c102 -m 1

After issuing the command an editor for editing the commit message opens up (see below). To accept the default message and exit the editor, press Esc and then ZZ.

Now a new commit has been added, which reverts the changes introduced by the merge commit.



In case you had a hard time following my explanation, maybe the answers in the sources I used can help you.