Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stage file by its file name, regardless of directory--Git

Tags:

git

I have very nested directories in a project, and I'm a lazy programmer.

Let's say I have a file name EventEditor.foo I want to stage my file regardless of whether it's in the root directory or ./src/holy/sweet/mother/of/baby/raptor/jesus/this/is/a/long/hiearchy/EventEditor.foo

My goal would be to be all, "Yo Git, add EventEditor" and bam. It stages it with me only having to type something like git add *EventEdi*. Is this possible? Or am I day dreaming?

like image 819
Jonathan Dumaine Avatar asked Sep 22 '10 23:09

Jonathan Dumaine


People also ask

How do I stage all files in git?

The easiest way to add all files to your Git repository is to use the “git add” command followed by the “-A” option for “all”. In this case, the new (or untracked), deleted and modified files will be added to your Git staging area. We also say that they will be staged.

How do I stage all files in a folder?

Stage all Files The -A option is shorthand for --all . Another way to do this would be to omit the -A option and just specify a period to indicate all files in the current working directory: $ git add . Note: The command git add .

How can you stage only some changes in a file?

In the Commit window, select the file you want to partially commit, then select the text you want to commit in the right pane, then right-click on the selection and choose 'Stage selected lines' from the context menu.

Does git add stage files?

That's where Git's add command comes in. We add files to a staging area, and then we commit what has been staged. Even the deletion of a file must be tracked in Git's history, so deleted files must also be staged and then committed.


2 Answers

If you would like to match a glob recursively when using git add, start the glob you pass in to git add with a directory name (such as . for the current directory), and make sure that the glob is in quotes so that Git can interpret it instead of the shell:

git add "./*EventEdi*"

A full example:

 $ git init git-add Initialized empty Git repository in /Users/lambda/tmp/stackoverflow/git-add/.git/ $ cd git-add/ $ mkdir -p foo/bar/baz $ touch foo/bar/baz/some-long-filename.txt $ git add "./*long-filename*" $ git status # On branch master # # Initial commit # # Changes to be committed: #   (use "git rm --cached ..." to unstage) # #   new file:   foo/bar/baz/some-long-filename.txt # 

From the manual:

Fileglobs (e.g. *.c) can be given to add all matching files. Also a leading directory name (e.g. dir to add dir/file1 and dir/file2) can be given to add all files in the directory, recursively.

like image 86
Brian Campbell Avatar answered Oct 01 '22 12:10

Brian Campbell


If you're on Linux, you can easily use something like :

find . -name EventEditor.foo -exec git add {} \; 
like image 22
Matthieu Avatar answered Oct 01 '22 10:10

Matthieu