Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore new commits for git submodule

Background

Using Git 1.8.1.1 on Linux. The repository looks as follows:

master
  book

The submodule was created as follows:

$ cd /path/to/master
$ git submodule add https://[email protected]/user/repo.git book

The book submodule is clean:

$ cd /path/to/master/book/
$ git status
# On branch master
nothing to commit, working directory clean

Problem

The master, on the other hand, shows there are "new commits" for the book submodule:

$ cd /path/to/master/
$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   book (new commits)
#
no changes added to commit (use "git add" and/or "git commit -a")

Git should ignore the submodule directory completely, so that the master is also clean:

$ cd /path/to/master/
$ git status
# On branch master
nothing to commit, working directory clean

Failed Attempt #1 - dirty

Inside the file master/.gitmodules is the following, as per this answer:

[submodule "book"]
        path = book
        url = https://[email protected]/user/repo.git
        ignore = dirty

Failed Attempt #2 - untracked

Changed master/.gitmodules to the following, as per this answer:

[submodule "book"]
        path = book
        url = https://[email protected]/user/repo.git
        ignore = untracked

Failed Attempt #3 - showUntrackedFiles

Edited master/.git/config to the following, as per this answer:

[status]
   showUntrackedFiles = no

Failed Attempt #4 - ignore

Added the book directory to the master ignore file:

$ cd /path/to/master/
$ echo book > .gitignore

Failed Attempt #5 - clone

Added the book directory to the master as follows:

$ cd /path/to/master/
$ rm -rf book
$ git clone https://[email protected]/user/repo.git book

Question

How can the book submodule be in its own repository directory under the master repository yet have git ignore the book submodule? That is, the following should not display:

#
#       modified:   book (new commits)
#

How to suppress that message when executing git status in the master repository?

An article about git submodule pitfalls suggests this an inappropriate submodule usage?

like image 205
Dave Jarvis Avatar asked Jan 19 '13 20:01

Dave Jarvis


People also ask

Does Gitignore apply to submodules?

No, you don't need to add your submodule to your . gitignore : what the parent will see from your submodule is a gitlink (a special entry, mode 160000 ). That means: any change directly made in a submodule needs to be followed by a commit in the parent directory.

What does New commits mean in git status?

This means you have new commits in that submodule, which have a new SHA1 reference. What you need to do in the master directory is commit those changes in the master repository. cd /path/to/master git commit . - m "Update 'book' in master"

How do I change a submodule to a specific commit?

Use the git submodule update command to set the submodules to the commit specified by the main repository. This means that if you pull in new changes into the submodules, you need to create a new commit in your main repository in order to track the updates of the nested submodules.

Why you should not use git submodules?

This is because of some major drawbacks around git submodules, such as being locked to a specific version of the outer repo, the lacking of effective merge management, and the general notion that the Git repository itself doesn't really know it's now a multi-module repository.


2 Answers

Just run:

$ git submodule update

This will revert the submodule the to old commit (specified in parent-repo), without updating the parent-repo with the latest version of the submodule.

like image 118
Shiv Kumar Avatar answered Oct 12 '22 20:10

Shiv Kumar


To include another repository, that needn't be tracked in its super-repo, try this:

$ cd /path/to/master/
$ rm -rf book
$ git clone https://[email protected]/user/repo.git book
$ git add book
$ echo "book" >> .gitignore

Then commit.

As stated in the linked git submodule pitfalls article:

... the only linkage between the parent and the submodule is [the] recorded value of the submodule’s checked-out SHA which is stored in the parent’s commits.

That means that a submodule is not saved by its checked-out branch or tag, but always by a specific commit; that commit (SHA) is saved into the super-repo (the one containing the submodule) like a normal text file (it's marked as such a reference, of course).

When you check out a different commit in the submodule or make a new commit in it, the super-repo will see that its checked out SHA has changed. That's when you get the modified (new commits) line from git status.

To eliminate that, you can either:

  • git submodule update, which will reset the submodule to the commit currently saved in the super-repo (for details see the git submodule manpage; or
  • git add book && git commit to save the new SHA into the super-repo.

As mentioned in the comments, consider abandoning the book submodule: clone it inside the super-repo, if tracking of its state as part of the super-repo is not necessary.

like image 63
Nevik Rehnel Avatar answered Oct 12 '22 19:10

Nevik Rehnel