1

Scenario: I have multiple files on GitHub under multiple branches (e.g Master / Dev / UAT etc.), in which I want to search and replace a string (e.g. "Connection").

Issue: Once the string has been changed (e.g. "Connection Oracle" to "Connection MySQL"), then the earlier string "Connection Oracle" is still stored in the git history.

How can I list all the files which have a particular string (e.g "Connection") in the git history.

mcls
  • 9,071
  • 2
  • 29
  • 28
Kbox
  • 21
  • 5
  • 1
    http://stackoverflow.com/q/4110652/6309 should help here – VonC Nov 26 '16 at 13:40
  • Are you asking how to do this using the *GitHub web interface*, or how to do this using the Git tools and a local clone of the repository? – torek Nov 26 '16 at 13:46
  • either way GitHub web interface or local clone shall be ok. I have access to both (Not sure about Github tools) – Kbox Nov 27 '16 at 08:44

3 Answers3

1

You can use git grep command i.e. git grep Connection $(git rev-list --all )

Please go through answer mentioned in below link

How to grep (search) committed code in the git history?

Community
  • 1
  • 1
  • Thank you i Could list down all the files with the perticular string, Now i want to change the string from the files eg. File1.txt has a string called "Connection" and i want to replace this with "Oracle_Connection". Please suggest how can i replace strings in files one by one.. – Kbox Nov 28 '16 at 04:48
  • do you really need to change the history ? generally we can make changes in file and commit it again.. –  Nov 28 '16 at 04:54
  • not advisable but in case you need to change the commit you can use git rebase command . Please refer below link for more details https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History –  Nov 28 '16 at 04:56
  • I tried "git filter-branch --tree-filter "find . -name '*test.sh' -type f -exec sed -i -e 's/Connection/Oracle_Connection/g' {} \;" I worked but i can see the files with "Connection" string still there in Github where the files with "Oracle_connection" has also been generated and getting below error Cannot create a new backup. A previous backup already exists in refs/original/ Force overwriting the backup with -f – Kbox Nov 28 '16 at 07:47
1

Maybe git log -SConnection but I think you be on the branch that you want to search on.

Here the doc about -S option for git log

-S Look for differences that change the number of occurrences of the specified string (i.e. addition/deletion) in a file. Intended for the scripter’s use.

Sr. Libre
  • 339
  • 4
  • 10
1

There is nice tool git grep

So just do:

git grep --files-with-matches Connection

IMO, for your case, it would be bast to apply this on each branch you need to update. Searching whole history will flood you with useless information. If you need to update master it is not wise to update branches which will be merged to master in later time, since it will introduce unneeded merge conflicts.

Marek R
  • 32,568
  • 6
  • 55
  • 140