Moving the .git directory wihout moving the files - linux

I am using to track changes to some linux system files (/etc/*), I had the .git in /etc
but now I decided to move it to / as I want to track files that are outside /etc (both /etc and / are in the same filesystems...), I did that and tried to re-add the same files with:
git add $(git status | awk '/deleted/ { print "etc/"$3 } ')
But it does not appear to be working as I hoped as now the are two lists one with a list of "new files" and one with a list of "deleted files", if commit now I will lose all the history
for the files....
What would have been the correct steps?
Thanks!
Antonio

Use git subtree (installation instructions if not already installed).
Create a new repository at / and merge it with the existing one in /etc:
$ cd /
$ git init
$ git subtree --prefix etc /etc master

Ther are similar questions on SO:
My Git repository is in the wrong root directory. Can I move it? (../ intead of ./)
Moving a git repository
And there is no way of chaning directory without losing history of files.

Related

Git delete all unmodified files

I am using git in my project at Linux platform. I have plenty of files in a particular directory. I modified some 50 above files in that directory and didn't stage and commit it. I wish to delete all other unmodified files from that directory? Is there a way to do this, using git and Linux Commands?
Not sure why you would want to do this.... but you can:
# Save changes to stash
git stash save
# Remove everything left
rm -rf ./*
# Checkout (restore) all of the changed files
git stash show --stat | grep -v changed | sed -e 's/|.*$//;' | xargs git checkout
# Restore the changes to those files
git stash pop
git reset --hard [HEAD] should work for you repeated
Repeated question How can I discard modified files?
You can also use more simple commands for this purpose:
git clean -Xfd // capital X
git clean -xfd // lower x
It will clean your working directory from the desired files.
Using git clean is what you want. To remove (-x) those files and directories (-d), run:
$ git clean -fdx
If you use the -X option instead of -x, then the files you have told git to ignore will still be kept (e.g., build artifacts). Recent versions of git require either "-f" (force) or "-n" (dry-run) to be specified.
You should run a dry-run first, to show what will happen, but not actually do anything:
$ git clean -ndx
I use this so often, that I have an alias for this (added to your .gitconfig) to check for files that would be deleted when you run git clean. It's also useful to remind me if I've forgotten to "git add" a file that I want to keep.
[alias]
# list files that would be removed via 'clean' (non-destructive)
ifc = clean -ndx
Then, running git ifc (i.e,. "ifc" = "if clean") shows everything that isn't tracked and could be removed, or isn't tracked and should be added.
https://git-scm.com/docs/git-clean

Get only structure of repo folders without files

I have some repo in perforce, I want to download only structure of folders without files, do you know how can I make this ?
Cheers
To learn about the folders/directories that are in a certain section of your Perforce repository, you can use the p4 dirs command (see http://www.perforce.com/perforce/doc.current/manuals/cmdref/p4_dirs.html).
For example,
p4 dirs //depot/*
will tell you all the top-level directories under //depot. Suppose the list that comes back is:
//depot/main
//depot/r1.0
Then you could subsequently issue:
p4 dirs //depot/main/*
and
p4 dirs //depot/r1.0/*
to learn about the next level of directories, and so forth, until you find no further child directories under the section of the repository that you are searching.
Once you have learned the correct set of directories that correspond to the current contents of your repository in Perforce, you can issue the corresponding mkdir commands to make those directories on your workstation.

Going back to the history of my working directory - git, bash

I used git commit command directly on a remote repo, then git reseting hard to HEAD^,and I don't know which files of the remote directory were deleted, is possible to find the history of the files of my remote working directory ? Perhaps a bash command would suffice ?
You can view what files were deleted with:
git log --diff-filter=D --summary | grep delete

Git branch and ls

I'm not sure if this is the right place to ask this, please redirect me if not.
I'm new to git and while learning it I stumbled upon this.
How does git branch branchName and 'ls' work with each other.
For eg:
If I have a master and test branch and test branch has an extra testFile when compared to master branch.
Now, while in the master branch, if I ls, I'll wont see the testFile but after switching to the test branch and ls, I'll see the testFile
kiran#kiran-desktop:/media/kiran/Linux_Server/dev$ git branch
master
* test
kiran#kiran-desktop:/media/kiran/Linux_Server/dev$ git checkout master
M editor/editor_parts/syntax/operator_syntax.js
Switched to branch 'master'
kiran#kiran-desktop:/media/kiran/Linux_Server/dev$ ls
cgi-bin index.php misc underConstruction
editor jquery-1.5.2.min.js php.php userManage
fileManage jquery-ui-1.8.11.custom.css projectManage userPages
images login test.php
kiran#kiran-desktop:/media/kiran/Linux_Server/dev$ git checkout test
M editor/editor_parts/syntax/operator_syntax.js
Switched to branch 'test'
kiran#kiran-desktop:/media/kiran/Linux_Server/dev$ ls
cgi-bin index.php misc test.php
editor jquery-1.5.2.min.js php.php underConstruction
fileManage jquery-ui-1.8.11.custom.css projectManage userManage
images login testFile.txt userPages
kiran#kiran-desktop:/media/kiran/Linux_Server/dev$
But pwd from both branches shows the same location.
So, how does switching branches change the output of ls ( which as I understand is a function of linux) ?
git checkout switches you from one branch to the other. To do this, it replaces the files in the checked out repository with the ones from the branch.
Your repository is effectively the .git subdirectory.
If you do a ls -a, you'll see it:
% ls -a -1
.git
.gitignore
...
Tracked files are stored in there. You normally only see the currently checked out branch. When you checkout a different branch, git grabs the files from .git and you can see them with ls.
Have a look at the answers to this question for more information about how git works: Where can I find a tutorial on Git's internals?
Git, unlike in SVN what you know keeps branches in different directories, keeps only the current working branch in the repository directory.
All the branches (Actually, all the objects) are stored inside the '.git' folder in the root of the repo and only the files belonging to the specific branch are present while you have checked out a specific branch. (and those files that are not added to the repo)

Checking changes made before/after installing application?

On Linux, I need to know which files were added/modified/moved/deleted after compiling and installing an application from source code, ie. the command-line, Linux equivalent to the venerale InCtrl5.
Is there a utility that does this, or a set of commands that I could run and would show me the changes?
Thank you.
Edit: The following commands are sort of OK, but I don't need to know the line numbers on which changes occured or that "./.." were updated:
# ls -aR /tmp > b4.txt
# touch /tmp/test.txt
# ls -aR /tmp > after.txt
# diff -u b4.txt after.txt
If you only need to know which files were touched, then you can use find for this:
touch /tmp/MARK
# install application here
find / -newercm /tmp/MARK
This will show you all files whose contents or metadata have changed since you touched /tmp/MARK (including newly added files).
I would personally use something like Mercurial (version control) to do this.
The main reason, is that it is not only effective but it is also clean, since it will only add a hidden directory to the top of the tree where you want to check these changes.
Let's say that you need to know what files changed in /etc/. So before installation (you need to have mercurial installed) you add the directory to mercurial:
cd /etc
hg init
hg add
hg ci -m "adding all files in /etc/ to track them down"
The above will effectively "add" all the files to track them. To verify nothing has changed:
hg st
Should return no files.
If you (or the installation) modifies a file, you should see something like this:
hg st
M foo.sh
The "M" before the file states the given file was modified.
For new files you would see a ? before the file like:
? bar.sh
After you are done and no longer want Mercurial, simple remove the hidden directory:
cd /etc
rm -rf .hg

Resources