Automatically remove files in GIT repository after renaming them - linux

I usually does the following with
$ mv file1.py file2.py
$ git add .
$ git rm file1.py
How can I skip the last step? So that whenever I add the newly name
files it will automatically remove the old one file1.py in the git repository.
I know I can do git mv file1.py file2.py. But in my workflow I tend to do many mv commands and prefer to add and commit them at the very end.

If you want to stage all current files you can do it with git add -u So you can try the following combination:
git add . #add new files
git add -u #stage removed files
Example
lol4t0#lol4t0-VirtualBox:~/gtets$ mv foo bar
lol4t0#lol4t0-VirtualBox:~/gtets$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: foo
Untracked files:
(use "git add <file>..." to include in what will be committed)
bar
no changes added to commit (use "git add" and/or "git commit -a")
lol4t0#lol4t0-VirtualBox:~/gtets$ git add -u
lol4t0#lol4t0-VirtualBox:~/gtets$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: foo
Untracked files:
(use "git add <file>..." to include in what will be committed)
bar
lol4t0#lol4t0-VirtualBox:~/gtets$ git add .
lol4t0#lol4t0-VirtualBox:~/gtets$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: foo -> bar

Related

Git #hashtags in status

I moved between two machines and now all my git status commands outputs are filled with #
I have the same .gitconfig and .bashrc files.
Also tested making a new git clone, same happens.
On my old computer it looks normal:
On branch xxx
Your branch is up to date with 'origin/xxx'.
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: xxx
On my new computer it's full of "comments":
# On branch xxx
# Your branch is up to date with 'origin/xxx'.
# 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: xxx
How do I remove those tags?
Older git versions just printed # in front of the lines in git status by default. And it was removed in newer version.
See https://github.com/git/git/commit/2556b9962e7c0353d562b7bf70eed11d8f29d0b0#diff-21ecaaf4e2318a39cdedf505941dbfbabd3329f041c839a03fb84906b4dae4fb this commit.
status: disable display of '#' comment prefix by default
Historically, "git status" needed to prefix each output line with '#'
so that the output could be added as comment to the commit message.
This prefix comment has no real purpose when "git status" is ran from
the command-line, and this may distract users from the real content.
Disable this prefix comment by default, and make it re-activable for
users needing backward compatibility with status.displayCommentPrefix.
Obviously, "git commit" ignores status.displayCommentPrefix and keeps
the comment unconditionnaly when writing to COMMIT_EDITMSG (but not
when writing to stdout for an error message or with --dry-run).
Signed-off-by: Matthieu Moy Matthieu.Moy#imag.fr Signed-off-by:
Junio C Hamano gitster#pobox.com

Git doesn't add my files, even though I add them

After adding files in a bunch of different ways, the files are still not added to the commit, no matter which way I am trying to add them. This is my console output so you understand better:
daniel#padamtuts:~/github/dotfiles$ 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)
(commit or discard the untracked or modified content in submodules)
modified: .vim/bundle/auto-pairs (modified content)
modified: .vim/bundle/ctrlp.vim (modified content)
modified: .vim/bundle/emmet-vim (modified content)
modified: .vim/bundle/nerdtree (modified content)
<a lot of files>
no changes added to commit (use "git add" and/or "git commit -a")
daniel#padamtuts:~/github/dotfiles$ git add *
daniel#padamtuts:~/github/dotfiles$ git add -A
daniel#padamtuts:~/github/dotfiles$ git commit -m "Vim bundles"
On branch master
Changes not staged for commit:
modified: .vim/bundle/auto-pairs (modified content)
...
<same files>
no changes added to commitPdaniel#padamtuts:~/github/dotfiles$ 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)
(commit or discard the untracked or modified content in submodules)
modified: .vim/bundle/auto-pairs (modified content)
<same files>
no changes added to commit (use "git add" and/or "git commit -a")
daniel#padamtuts:~/github/dotfiles$ git add *
daniel#padamtuts:~/github/dotfiles$ git add -A
daniel#padamtuts:~/github/dotfiles$ git commit -m "Vim bundles"
On branch master
Changes not staged for commit:
modified: .vim/bundle/auto-pairs (modified content)
<same files>
no changes added to commit
```
Files/directories that begin with . are 'hidden' and will be ignored by git when you add with the glob character. You can read about these 'dotfiles' if you want to learn more; usually they are configuration files (e.g. for vim) and such that you may not want to interact with directly and by default will not appear in a file explorer, or in the output of ls, etc.
For your purposes, git add . should do the trick.

Phabricator: Error "ARC: Cannot mix D and F" when do arc diff

I did a commit and after trying to crate a diff with
arc diff dev command but I've got an error
ARC: Cannot mix D and F
UNIX: No such file or directory
Any ideas how to solve it?
The steps I did before arc diff command:
$ git commit -am "<commit message>"
$ git pull --rebase
$ git push origin my_working_branch
The git status output:
$ git status
On branch ATD-86
Untracked files:
(use "git add <file>..." to include in what will be committed)
../javaproject/
nothing added to commit but untracked files present (use "git add" to
track)
The branch dev is protected, could it be a reason?
This error message seems to come from the arc command at https://github.com/hyc/arc/
This is not the arc command you want for using Phabricator's arcanist. Try export PATH=/path/to/arcansit/bin:$PATH to prepend arcanist to your path before that foreign arc command, or use alias arc='/path/to/arcanist/bin/arc' to force the correct arc command.

How to revert a file in Perforce?

In Perforce, how do I revert changes I have made a file?
$ p4 status
UI\FormMain.cs - reconcile to edit //UI/FormMain.cs#73
$ p4 revert UI/FormMain.cs
UI/FormMain.cs - file(s) not opened on this client.
In Git I do this with the command git checkout.
$ 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: Form.cs
#
no changes added to commit (use "git add" and/or "git commit -a")
$ git checkout Form.cs
$ git status
# On branch master
nothing to commit, working directory clean
If the file isn't opened, yet you have made changes to it locally, use
p4 sync -f UI/FormMain.cs
to tell Perforce to forcibly (-f) sync the copy of FormMain.cs that you have on your machine with the version from the server.
Instead of p4 revert UI/FormMain.cs you should call p4 revert //UI/FormMain.cs since that's the absolute path in depot notation.
The file with out the leading // marks a file relative to your current location in the client workspace.

How to "attach" working dir to bare GIT repository

I have embedded Linux system that we want to store in Git. I have installed Git on the system, mount additional USB drive for storing Git data (bare repository). There is no problem with committing and pushing to the remote repository using commands like that:
cd /media/usb
git init --bare
git --work-tree=/ add -A
git --work-tree=/ commit
git --work-tree=/ push -u origin master
But when I clone bare repository to new USB drive and invoke git --work-tree=/ status I see all previously pushed files as deleted, and untracked. How to tell Git to use the work-tree?
The reason you are seeing previously committed files as deleted is that the git index (which is simply a file called index) in the first repository differs from the index in the second repository. The index in the first corresponds to the working tree, whereas the index in the second is uninitialized and therefore has no entries. The output from git status is the result of two comparisons:
between HEAD and the index (to determine staged changes to be committed)
between the index and the working tree (to determine unstaged changes which will not be committed)
In your case, HEAD in the second repository points to a commit which contains all the files you committed from your root filesystem, but the index is empty. So when git performs the first comparison, it thinks that each of these files has been staged for deletion on the next commit.
When git performs the second comparison, it finds that the working tree contains all the same files as the commit, but the index is of course still empty, so it sees these files as "new" untracked files. That is why you see all the files as both deleted and untracked.
The solution is very simple: initialize the second index so that it matches master:
git --work-tree=/ reset
While I'm here, I should point out some other issues with the commands you posted:
Firstly, your git add -U is adding all the git repository meta-data files to the repository. In other words, the repository is tracking itself. This is happening as a consequence of the way you use --work-tree, and is very bad. You should ensure that the repository files are ignored by adding them to info/exclude or .gitignore.
Secondly, you don't really want a bare repository here, just a detached working tree. You could have achieved this via git config core.bare false and export GIT_DIR=/media/usb; then you could run git commands from outside (i.e. above /media/usb), and you wouldn't have to continually include --work-tree=/ as a global option in each command.
Here's a complete test case which encapsulates everything I just covered except for the second bullet point:
#!/bin/sh
root=fakeroot
mkdir -p $root/media/usb{1,2} $root/{bin,etc}
echo a > $root/bin/sh
echo b > $root/etc/hosts
cd $root/media/usb1
git init --bare
# We don't want our git repository meta-data being tracked.
echo '/media/usb*/' >> info/exclude
git --work-tree=../.. add -A ../..
git --work-tree=../.. commit -m '1st commit'
echo c >> ../../etc/hosts
git --work-tree=../.. add -A ../..
git --work-tree=../.. commit -m '2nd commit'
git remote add origin ../usb2
git --git-dir=../usb2 init --bare
git push origin master
cd ../usb2
echo '/media/usb*/' >> info/exclude
echo "========================================="
echo "index in usb2 is not yet initialized:"
git --work-tree=../.. status
echo "========================================="
echo "initialize index to master (HEAD)"
git --work-tree=../.. reset
echo "========================================="
echo "now we have a clean working tree:"
git --work-tree=../.. status

Resources