Tuesday, December 7, 2010

Processing files with certain patterns

While I was working on some project, I wanted to move backwards to an old branch to check how an old feature used to work.. and as I normal attitude when I switch to a branch I sync it up with the master branch.. and git just returned

# Your branch and 'origin/master' have diverged,
# and have 3 and 475 different commit(s) each, respectively.

ohh.. I forgot that I don't need most of the newly added modules to the project and I should remove some of them, also many conflicts between files..

OK, I thought to just delete/revert some changes to let my branch work.. yet doing this manually would be a very ridiculous task and would consume much time..

Yet, on shell you can do that with just few commands..
So, lets see how can we do that..

1- Get change list from git
(short hand for git status) would return whole list of changes
git st


# On branch BRANCH_X
# Your branch is ahead of 'origin/master' by 9 commits.
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
# file1.x
# file2.x


2- redirect to awk
git st |  awk '{print $0}'

same output from step1 but returned by awk

3- filter the files change list by awk 
git st | awk '/<your_filter_pattern>/ {print $0}'

for example: simple filtering for '#\t+' (regular expression for: a # followed by one or more tabs)
git st | awk '/#\t+/ {print $0}'

returns
# file1.x
# file2.x
...

3- extract file names only in awk (remove heading # and tabs)
git st | awk '/#\t+/ {gsub(/#\t+/,"", $0); print $0}' 

returns
file1.x
file2.x
...

4- redirect awk output to shell to remove/revert these files
xargs <shell_command>

git st | awk '/#\t+/ {gsub(/#\t+/,"", $0); print $0}' | xargs rm

would remove all files returned in step3

Useful links

No comments:

Post a Comment