git on Linux uploading files from another repository too - linux

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.

Related

pushing and pulling from remote git repo works but it is empty on remote server [duplicate]

This question already has answers here:
What's the -practical- difference between a Bare and non-Bare repository?
(11 answers)
Closed 2 years ago.
I have a linux server with ssh and git installed.
I created a folder on the root directory and changed its owner to mike to avoid giving out root passwords to anyone.
sudo mkdir /GitRepos
sudo chown mike:mike /GitRepos
Then I created an empty repo, and set it to bare, otherwise pushing to this repo does not work (I tried!!)
mkdir test.git
cd test.git
git init
git config core.bare true
I then used another machine (Windows) to clone the newly created repo:
git clone ssh://mike#myLinuxServer/GitRepos/test.git
and that worked fine, I got an empty repo as expected.
I added some files and committed the change.
git add .
git commit -m "Initial commit"
git push origin master
Pushing works fine. I even deleted the entire repo from the local machine, and tried to clone it again.
cd ..
rd test /s
git clone ssh://mike#myLinuxServer/GitRepos/test.git
It worked fine and I got all of the files.
When I went to the remote Linux machine, I listed the files inside test.git and found it empty.
Where did the files go?
How can the repo receive pushes and give out files for pulls without storing the files in the repo?
The files are stored in the remote repository's database. You cannot expect that the files themselves appear in the remote repository, in particular not when you declare it as bare.
The way you initialized the bare remote repository is very unusual. You should have done it simply like this:
cd /GitRepos
git init --bare test.git
The repository on the server is considered as a bare repo as you ran git config core.bare true. A more common way to initialize a bare repo is git init --bare test.git or mkdir test.git;cd test.git;git init --bare. The difference between git init test.git;cd test.git;git config core.bare true and git init --bare test.git lies in the directory structure. The former creates the worktree(test.git) and the database(test.git/.git) but forbids the worktree then. The latter creates only the database(test.git), without the worktree from the beginning.
A bare repository does not have an active worktree by default, which means you can't check out files from any revision in a bare repository. Although you can create extra worktrees with git worktree add, it's quite rare and may behave not so well as a non-bare repository with extra worktrees. All the revisions are stored as compressed data in the database. You can use commands to read the data, like git show master or git ls-tree -r master.
otherwise pushing to this repo does not work (I tried!!)
It's partially true. You can't push to a non-bare repository when you try to update a branch which has been checked out in the remote repository. If the branch you try to update is not checked out or does not exist yet, you can push to update or create it. With git config receive.denyCurrentBranch ignore or git config receive.denyCurrentBranch warn, you can even update such branch. But in practice, we always use bare repositories to preserve revision data.

how to sync data from local code to heroku git

I build some website using nodeJS that host on heroku. I am using the heroku CLI git. When i want to upload the files i am using the git add . after that git commit -am "some text" and git push heroku master.
The client is update his site and upload new images and content, and when i make some changes in the code and git it again all the work he is done is deleted from server (not from DB).
Add the images folder to the .gitignore file
How can i sync files and code with my local version before i push new version to the heroku server
when you do "git add ." you are telling git to overwrite all your files from git with code on you local.
if you have made changes in only specific files You should always add only those files when doing git add .
you can follow this steps.
git status -> this will show you list of files which are changes and umerged. get hold of file which you changed and want to add in git
Suppose its abc.html (copy full path of file shown in git status)
git add abc.html
git commit -m "some message"
(optional) git pull remote_branch current_branch (this will pull latest code of remote branch to your local). Sometimes you may get conflicts so go ahead open that file and resolve conflicts.
If you are alone working on branch you may not get any conflicts
git push current_branch remote_branch

How do I "extract" my commited files?

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

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...

"git add" returning "fatal: outside repository" error

I'm just entering into the wonderful world of git.
I have to submit a bunch of changes that I've made on my program, located in a directory called /var/www/myapp.
I created a new directory /home/mylogin/gitclone. From this directory, I did a git clone against the public repo and I was able to get the latest copy created.
I'm now trying to figure out how to take all the files in my working folder (/var/www/myapp) and "check them in" to the master repository.
From /home/mylogin/gitclone, I tried git add /var/www/myapp but I'm getting an error that the folder I tried to add is outside the repository.
Can you give me a few pointers on what I'm doing wrong? Also, I'd like to add everything, whether it's different from the master or not.
Thanks.
First in the clone folder you can create a Branch (so the master stay untouched)
git branch [branch_name]
After, just copy the files you want from your old folder to the clone folder.
When you are done, just add / commit your change and Merge your branch into the "master" branch. It will look like to something like this:
git add .
git commit -m "Comments"
git checkout master
git merge [new_branch]
Try this tutorial from GitHub.
You'll have to move all the files from /var/www/myapp to /home/mylogin/gitclone and then do a git add . and then git commit -m "Your message".
When upgraded to git version 2.12.2 that error appeared, I nooted the i add the file with a full path like:
git add c:\develop\project\file.text
when removed the full path it start working, like:
git add file.text
To add some files or folder to your repository, they have to be in the folder you created with git clone. So copy/paste your application in your local git folder and then go in it and do git add * and then you'll be able to commit to the server with git commit -m 'message' and finally push the changes to the server with git push
Okay, this error came up for me because I moved the project from one computer to another.
So the git was not able to figure my global git user.name and user.email
I opened the command prompt and specified my old git user.name and user.email from previous computer. Kindly run the following commands and it should be fixed.
cd pathToMyProjectDirectory
git config user.name "myName"
git config user.email "myEmail"
That's because you are versioning stuff inside /home/mylogin/gitclone and git tracks everything inside that folder. You cannot track other folders outside of this repository.
A solution might be create a submodule, or using a symbolic link using ln -s
Git only tracks files and folders within the root folder which includes the .git directory and the subfolders inside root folder. The folder you are trying to add is outside the scope of git.
What would you actually like to do is first git checkout -b myapp which will create and checkout a new branch based on the master branch of the repository you cloned. Then you would actually copy all your files over and commit them with git commit -a -m "Short descriptive name about what you did". The parameter -a you passed to git commit is for including all the changes done to the repository and -m is to include the commit message in the actual command. After that you can either push back to the main repository if you have write access to it or push it to your own public repo or don't push it at all.
What I've described above is pretty much the basics of git. Try reading this book which is pretty descriptive.
Maybe someone comes along having the same trouble like I had:
In my case this error was thrown while using husky (commit hooks) https://github.com/typicode/husky
It was just an error because of encodings. My source was located in a directory that contains a special character ("รถ")
Seems like husky uses "git add" with the absolute path of the source which fails somehow at this point
I renamed the path and it worked fine.
This message can also appear when the file name is listed in the .gitignore file.
My scenario is that the git repository's path has symbolic link and git throw out this error when add file say to "/home/abc/GIT_REPO/my_dir/my_file".
and "/home" is actually a softlink to "/devhome".
code ninja gave me some light when I tried to debug this case.
So I tried get the target directory by using the command readlink -f /home/abc/GIT_REPO before run add command.
And then everything works like a charm !
I encountered the issue at Windows box with maven-release-plugin.
The plugin tries to add files using absolute path and I have noticed that the path in the Git add command starts with uppercase D: while the path in Working directory: log line started with lowercase d:
I have added core.ignorecase = true to the Git settings and the issue was gone.

Resources