How do I "extract" my commited files? - linux

I have two files file1.c and file1.h in my working directory /tmp/working. Everything I do is on my local file system.
I do a git init in /tmp/working that creates a .git directory in it. Then I git add file1.* and git commit -m "Feb 13th 2017".
On Feb 17 I accidentally delete the two files in my working directory. How do I restore my files in my working directory from my local git repository? I don't want to undo the last commit or something like that, just want the copy of my files (version of Feb 13th) back in my working directory.

You can try just checking out those two files from the latest commit:
git checkout -- path/to/file1.c
git checkout -- path/to/file1.h
The nice thing about Git is that it is hard to really mess things up. You only deleted those two files locally in the current commit. But their history is easily accessible using git checkout.
Actually, any path works:
git checkout -- path/to/ # extracts the whole "path/to" directory
git checkout -- . # extract all the content of the last commit
and you can also specify any commit :
git checkout other_branch -- path/to/ # extracts content from other_branch
git checkout v1.7.3 -- path/to/ # from this tag
git checkout eacf33b -- path/to/ # from this commit

Related

Git status content is different in different linux account

There are two accounts in linux server, root and super.
I can git pull to latest code by root account
I cannot git pull to latest code by super account
I found the status content is different by execute git status
super#api-dev:~/dnmp/www/localhost$ git status
On branch develop
Your branch is up to date with 'origin/develop'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
../../data/
../../docker-compose.yml
../../services/
../
nothing added to commit but untracked files present (use "git add" to track)
...
root#api-dev:~/dnmp/www/localhost# git status
On branch develop
Your branch is up to date with 'origin/develop'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: composer.lock
no changes added to commit (use "git add" and/or "git commit -a")
There is a git repository root in ~/dnmp/
There is another git repository root in ~/dnmp/www/localhost
I want to change git status of super account to the same as root content.
Any idea to fix the difference?
git is different from other source control systems. When you clone a repository, it creates a local repository copy on your computer. So you have 1 copy as root, and another copy as super.
Then you can modify the files in your local copy, even commit these changes. But the main repository will not know of these until you push them into it.
So, when you run git status it shows you the state of the local repository, not the state of the main git repository.
To sync your local repositories with the main one, you can:
delete your local repository and clone from scratch.
pull the files from the main repository with git pull.
If you want to keep some changes you made locally, you will have to commit and push these up to the main repository first.
You can read on git, like this simple page: https://rogerdudler.github.io/git-guide/
More in depth sites exist to explain the whole of git, like:
https://www.atlassian.com/git/tutorials
https://www.freecodecamp.org/news/what-is-git-and-how-to-use-it-c341b049ae61/

git on Linux uploading files from another repository too

I have 2 git repositories, one with 3 files and another one with the same 3 files but similar content. (Like README.md, index.html, etc.)
I used "git add" to add the files and created 2 remotes named "view" and "music".
I added the 3 first files in the first folder with "git add" and commited+pushed them to the view repository.
Then I went to the other folder, added those 3 files with "git add" too and commit+pushed them too, but i suddenly have the other 3 files pushed too.
(The repository "music" has the files from "view" too)
TL;DR: How can I seperate git repositories on Linux
Running CentOS 7 and the newest git update available via yum.
I already tried making 2 different folders and writing "git init" before using the "git add" and other commands to commit + push.
They still returned an error instead of posting duplicates.
The following was used in both folders, but errors happened on the second folder
echo "# testing" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git#github.Username/reponame.git
git push -u origin master
I expected that git would somehow distinguish between multiple repositories, and that I can simply have 2 folders with 2 repositories.
I thought after a git push / commit the "git add array" of files would be empty again and I could add new files for another git commit / push.
How can I commit/push to multiple repositories via the Linux Command Line and push different files to different repositores without them getting mixed up.
EDIT:
The folder structure is as follows:
[usr#servr~]$ ls music/
index.html README.md songs
[usr#servr~]$ ls view/
index.html README.md pics
The exact steps I did:
cd music
git init; git add README.md; git add index.html; git add songs
git commit -m "Initial Commit"; git remote add origin <link2music>; git push -u music master
cd ../view
git add README.md; git add index.html; git add songs; git commit -m "Initial Commit"; git remote add origin <link2view>; git push -u view master
Then the git repository of view suddenly had the README.md of the music repository, and the folder songs too (even though it shouldnt).
I deleted the view repository but kept the music repository on github. (No folder deleted on my Linux machine). I then tried to exactly enter the code from the first snippet (the github offical one) in both folders (with git init too in each folder) and now the music repository, which already existed, threw me the error "! [rejected] master -> master (fetch first)", even though i never deleted the music repository, neither from my machine nor from github. (files are the same on both)
Someone recommended me to try git status, and with that I found out that my problem was the upper-level folder. I accidentally made the upper level folder, above my 2 "repo"-folders, a git repository.
How it should be:
Home
->music(repo .git)
->view(repo .git)
Hot it was (bad):
Home (repo .git)
->music
->view
That means that I didn't push the 2 folders to their respective repositories, but I instead pushed the upper-level folder (my home folder) to the repositories, which of course contained both of my other folders.
I deleted the .git folder from my home, cloned my git repos in a different folder again, and now they work as intended.

How to get all deleted local files from remote using GIT

I have a local directory with multiple files. These files
are already stored in remote git (BitBucket).
I accidentally deleted these files locally, how can I
get it back from the remote repository?
You don't need to get them from remote if you already had those files locally.
You can use:
git checkout -- <deleted file name>
You can also use file and directory wildcards:
git checkout -- dir1/*
git checkout [<tree-ish>] [--] <pathspec>...
Overwrite paths in the working tree by replacing with the contents in the index or in the <tree-ish> (most often a commit). When a <tree-ish> is given, the paths that match the <pathspec> are updated both in the index and in the working tree.
Did you commit the deletions?
If not, you can use git checkout -- file to recover the files.
If you did, you can git reset to a previous commit where the files still existed.
If you need a more fine-tuned approach, you can git clone your remote repository to a new directory and then use file operations to copy files from the copy to your original repository.
You can reset your local master branch to the remote repo master like so:
git fetch
git reset --hard origin/master
If you just deleted the file and do not run git add, then
git checkout -- <file>
If you have run git add, then
git reset HEAD <file>
git checkout -- <file>
If you have commit it to local repository, then
git reset --hard <the_commit_before_bad_commit>
Anyway, you'd better backup all the files when you try it.

git add . adds changes of other projects too

I am trying to add all changed files to GitHub by using git add . but it also adds the changes made by me on other local projects. It should ideally add the changes of the same project.
How should it be solved?
When I run git status then I get to see those extra files which are unstaged.
Images:
I had a look at the image you uploaded and it seems like you have a weird git setup.
I see that you are inside the folder, ~/AndroidStudioProjects/ApiExample/. So this is your project directory?
But it looks like you have initialized a git repository in your home directory (i.e. ~/) and when you run git status inside the current path, it shows everything relative to that home directory path.
In my opinion, you shouldn't have done that, and should have maintained a separate and an independent git repository for each project.
My suggestion to fix your issue is to remove the .git directory from your home. I guess you don't have any valid projects in the root of your home dir?
First change your path to home directory:
cd ~
Then take a backup of your .git folder in case you need it.
cp -r .git .git-backup
Then remove it.
rm -rf .git
Now go back to your AndroidStudioProjects, there also you shouldn't be having a git repository.
cd AndroidStudioProjects
Remove the .git folder.
rm -rf .git
Finally, go to your ApiExample project folder, and initialize a new git repository there.
git init
Now everything should be fine. But you should know that this is only a suggestion to clear your mess. If you really had a valid git repository inside your home directory, or AndroidStudioProjects directories, then you need to use Git Submodules.
Hope it helps. Feel free to ask any doubts.
Each repo is in its own directory. Then when you cd myproject and run git add . it should only add files changed in this repo.
By Project, if you mean different files directory in the same repo then you should be using one of the below commands to match your needs
For Git version 2.x
git add -A stages All
git add . stages new and modified, without deleted
git add -u stages modified and deleted, without new
Detail:
git add -A is equivalent to git add .; git add -u.
The important point about git add . is that it looks at the working tree and adds all those paths to the staged changes if they are either changed or are new and not ignored, it does not stage any 'rm' actions.
git add -u looks at all the already tracked files and stages the changes to those files if they are different or if they have been removed. It does not add any new files, it only stages changes to already tracked files.
git add -A is a handy shortcut for doing both of those.
You can test the differences out with something like this (note that for Git version 2.x your output for git add . git status will be different):
git init
echo Change me > change-me
echo Delete me > delete-me
git add change-me delete-me
git commit -m initial
echo OK >> change-me
rm delete-me
echo Add me > add-me
git status
# Changed but not updated:
# modified: change-me
# deleted: delete-me
# Untracked files:
# add-me
git add .
git status
# Changes to be committed:
# new file: add-me
# modified: change-me
# Changed but not updated:
# deleted: delete-me
git reset
git add -u
git status
# Changes to be committed:
# modified: change-me
# deleted: delete-me
# Untracked files:
# add-me
git reset
git add -A
git status
# Changes to be committed:
# new file: add-me
# modified: change-me
# deleted: delete-me
Based on the image you gave, it appears you should be in the Silencio directory. Git is showing you that you have untracked files.
Try this:
cd ~/Silencio
git status
The output will show there are only changes for that directory.

How can I verify git clone is working correctly?

I'm following the documentation provided here by git to setup a bare git repository in a folder called root.
I started in the root directory where I ran
git init
git -A *
git commit -m "test"
I then ran git status and all appears good.
Next I ran the line from the documentation at a directory one level above the repo I created above.
git clone --bare root root.git
This created root.git but I cannot see any evidence that anything was cloned I just see a set of files and directories when I cd root.git.
I don't know how to verify it was actually cloned, and if it was why can't I see the original files?
When you do --bare --- you are telling git to clone just the git portion -
This is the option you use when you want to have a remote repository that does not include a workspace.
If you want to verify that it actually cloned your changes, you'll want to clone it again in a different directory - without the --bare flag
I would recommend using the full path to do this:
cd /path/to/some/workspace
git clone /path/to/your/root.git successful-git-clone #that last bit is optional
This will put the workspace contents of root.git into a folder named successful-git-clone/ - without that last bit, it will default to root/ -
Even if you are in a bare repository, some git commands works and you could do a git branch to see if you have all your branches or git log to look at your commits...

Resources