Linux

git: How to revert a file to a specific commit

This entry is part 7 of 7 in the series git: useful HOWTOs
git checkout COMMIT_HASH file/to/revert

Will bring the file/to/revert file to the state in COMMIT_HASH commit.

git: Completely revert the last commit

This entry is part 6 of 7 in the series git: useful HOWTOs
git reset --hard HEAD~1

will completely revert the last commit (i.e., everything from this commit will disappear).

git: How to checkout a specific commit/version to a new branch

This entry is part 5 of 7 in the series git: useful HOWTOs
git checkout -b branch--new commit-hash

will checkout the commit-hash commit in a new branch called branch-new.

git: How to push a new branch to a remote repository

This entry is part 4 of 7 in the series git: useful HOWTOs
git push -u REMOTE BRANCH_NAME

This will also create an association between the local and the remote branch, so you can afterwards just issue git push.

git: How to merge two commits into one

This entry is part 3 of 7 in the series git: useful HOWTOs
git rebase -i

and then pick the one you want to keep and squash the other

git: How to see the changes of a file for a commit

This entry is part 2 of 7 in the series git: useful HOWTOs
git show [commit] -- FILE

git: How to view all files of a commit

This entry is part 1 of 7 in the series git: useful HOWTOs
git show --name-only [commit]

Pattern Matching Hyphen-Minus Sign in Bash

I was trying to use the sed command to perform some changes to a text and stepped into an interesting “problem”; pattern matching the minus-hyphen (-) symbol.

Assume we have the following text:

something
SoMeThiNg
some-thing
soMe_thing

and we want to match all the different versions of the word with one expression (one by one).

My initial idea was to use this regular expression:

's/[a-zA-Z\-\_]*/matched/'

Naturally, I tried to escape the – sign. As you can see from the output, this doesn’t work:

$ sed 's/[a-zA-Z\-\_]*/matched/' test 
matched
matched
matched-thing
matched

The minus sign is not matched, because of its special meaning (setting ranges). In order to make the expression work, you need to move the “-” either in the beginning or in the end of the expression:

$ sed 's/[a-zA-Z\_-]*/matched/' test 
matched
matched
matched
matched
$ sed 's/[-a-zA-Z\_]*/matched/' test 
matched
matched
matched
matched

and leave it un-escaped!