How to update the directory in git

5.3k views Asked by At

I have a bitbucket repository, with the following structure :

 Directory-A->
   directory a
   directory b
   file a
   file b
   file c

Now I want to move file "b" and file "c" to "directory a"

When I do it on local machine and commit this by git add. I get file b and file c in directory a but they still exists outside it.

5

There are 5 answers

0
VonC On

When I do it on local machine and commit this by git add. I get file b and file c in directory a but they still exists outside it.

That is because you did a git add . in directory a instead of a git add -A . in Directory-A.

First, you need to be in Directory-A (the root one) when you do the git add -A . (which is a git add -u + git add .) from the root folder:

Directory-A->  <====== there: git add -A .
   directory a
     file b
     file c
   directory b
   file a

A 'git add -A .' in Directory-A will detect the deletion of those files in Directory-A and their addition in directory a.

See more at "Difference between “git add -A” and “git add .":

0
shoan On

This should work.

git mv "file a" "directory a"/

git mv "file b" "directory a"/

git commit -m "Files a and b are moved to directory a"

Happy Coding.

0
Zenexer On

Quick fix. Run:

git add -A :/
git status

Then make sure that only files you want to add/delete/modify have been staged. That's very important. If you've accidentally staged extraneous changes, run git reset. If you're sure you want to proceed, run git commit.

Without -A, git add only stages file additions and modifications. It will not delete entire files, and moves will turn into copies. git add -A deletes files that no longer exist.

:/ is a shortcut that means the root of the repository, rather than the current directory. If your current directory is the root of the repository, you can omit this.

Note that running git add -A--especially in the root of a repository--is generally considered dangerous. Do not get into the habit of blindly committing every change you've made: only stage what you know you want to change. However, when you're moving a large number of files around, this is quite a handy utility.

0
mikhail-t On

I usually do it like this, I believe it's even easier and will work for entire project:

  1. Physically move files b and c into directory a (using file explorer or command line).

  2. Run this Git command in your project:

    git add -u

    (it will auto remove all deleted files from Git index: since you moved your files physically Git will consider them 'deleted' from old location and created at the new one)

  3. Now add all changed/created files to new commit:

    git add .

  4. Commit changes as usual:

    git commit -m "Moved files b and c into directory a"


Here is more info on git add -u command (-u is a shorthand for --update):

... removes as well as modifies index entries to match the working tree, but adds no new files ... all tracked files in the entire working tree are updated ...

Source: http://www.git-scm.com/docs/git-add (look for --update section)

0
psiyumm On

Here is a better alternative. Try the following:

git mv "file b" "file c" "directory a"

Followed by:

git commit -am "hey I moved filed"

Note: I've only used the -am flag for demonstration purposes. It is not a good practice. You should commit only files with related changes together which makes your repository maintainable.