How to update directory in git

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 this on the local machine and do it with git add. I get file b and file c in directory a, but they still exist outside of it.

+7
git git-commit
source share
5 answers

When I do this on the local machine and do it with git add. I get file b and file c in directory a, but they still exist outside of it.

This is because you performed git add . in directory a instead of git add -A . in Directory-A .

First, you must be in Directory-A (root) when you do git add -A . (which is 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 detect the deletion of these files in Directory-A and their addition to directory a .

See the section " The Difference Between" git add -A "and" git add . " :

+4
source share

Here is the best alternative. Try the following:

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

Followed by:

 git commit -am "hey I moved filed" 

Note I used the -am flag for demo purposes. This is not a good practice. You should only commit files with related changes, which makes your repository convenient.

+4
source share

I usually do it this way, I find it even easier and will work for the whole project:

  • Physically move the b and c files to the a directory (using explorer or command line).

  • Run this git command in your project:

    git add -u

    (it will automatically delete all deleted files from the Git index: as you move the files physically, Git will consider them β€œdeleted” from the old location and created on the new one)

  • Now add all the modified / created files to the new commit:

    git add .

  • Replace changes as usual:

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


More on the git add -u command ( -u is short for --update ):

... deletes and also modifies index entries in accordance with the working tree, but does not add new files ... all monitored files in the whole working tree are updated ...

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

0
source share

Quick fix. Run:

 git add -A :/ git status 

Then make sure that only the files you want to add / delete / modify have been delivered . It is very important. If you accidentally make extraneous changes, run git reset . If you are sure you want to continue, run git commit .

Without -A , git add only adds and modifies files. It will not delete entire files, and moving will turn into copies. git add -A deletes files that no longer exist.

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

Note that running git add -A - especially at the root of the repository - is usually considered dangerous. Do not get into the habit of blindly making all the changes you have made: just put on what you know you want to change. However, when you move a large number of files, this is a pretty handy utility.

0
source share

That 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
source share

All Articles