How to start new Git from scratch when old Git still there - bash

I want to create a new empty repository with git. I cannot find a simple solution online that explains this step.
This documentation states "git-init - Create an empty Git repository or reinitialize an existing one" but then doesn't say which options are needed to make it empty:
https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-init.html
Is a --bare repository the same as an empty repository?
Problem:
I have two different projects with different repo's on github, but when I try to use Git (in Bash) after changing the directory to the new project, it keeps pushing with files from the old project.
I think the problem is that git is using the old repo files and thinks the new folder is just additional files perhaps? Basically I want to start from fresh. Can I just start from scratch on my new project with a new repo?
I have tried $ git init in a new directory, but then it just says: "Reinitialized existing Git repository in /home/user/new_project/.git/"
I tried: $ git remote set-url origin git#github.com:User/New_Project.git
but that just updates where it pushes my new project to, and then includes old projects files.
Please help a noob trying to figure things out the hard way 🙏🏼

Delete .git folder:
rm -rf .git
Then create new git repository:
git init
git remote add origin <remote-URL>
git add .
git commit -m "new clean repo"
git push --force origin master

create 1st project
make git init
create a remote repo for 1st project
link remote repo to 1st project
create 2nd project
make git init
create a remote repo for 2nd project
link remote repo to 2nd project
Git will push everything to its proper remote repo

Small hint from my side
git init : create empty repository and reinitialize an existing one
following link will give more information about different options can be used during git init ($GIT_DIR)
https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-init.html

Related

Github how to push Xcode project without old files and folders?

I finished an Xcode project and pushed it to github from Xcode 'Source Control' Menu. Then I changed the project name CountryBook to Countries. I Built project and ran. Everything was okay. Then I coiped project folder to desktop as a backup. Then pushed project again. Everything has messed up. Some old named folders and files still exist in repo. Then I deleted every directory and file from github repository. Now, backup version of project is working. But when I try to push it to repo, old files are still exist. I deleted 'origin' from 'Remotes' and created a new repo named 'Countries'. I pushed project again but it was same. A mixed version of old files and new files. When I clone the github version of project, of course it is not runnable. What sould I do and how can I push clean version of my project? I don't want to lose project.
This is Countries repo now:
This is my working project folder with correct content:
I would fix it via command line, lets assume you start from scratch:
Step 1 - prepare the working branch:
Clone the project
Navigate to root folder of the project
Checkout the main branch ("main", "master", or whatever it is)
Create a new branch you will be working with
git clone https://github.com/yourorg/yourrepo
cd yourrepo
git checkout main # or master
git checkout -b fixprojectstructure # branch name can be anything
Step 2 - clean project locally
Delete old project, old workspace, ensure the names in Podfile and Podfile.lock are fine
Build the project and ensure it's working
Step 3 - commit your changes:
# assuming you are in the root of the project
git add .
git commit -m "Some explanation"
Step 4 - push your changes:
I usually do it the lazy way: just run git push, which will show you the proper syntax to push remotely, something like
> git push
fatal: The current branch fixprojectstructure has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin fixprojectstructure
> git push --set-upstream origin fixprojectstructure
Enumerating objects: ...
...
remote: Create a pull request for 'fixprojectstructure' on GitHub by visiting:
remote: https://github.com/.../pull/new/fixprojectstructure
remote:
...
Branch 'fixprojectstructure' set up to track remote branch 'fixprojectstructure' from 'origin'.
Step 5 - merge your changes:
Basically just do what the line above is saying:
Navigate to https://github.com/.../pull/new/fixprojectstructure
Create pull request
Merge pull request to main (or whatever the initial branch was)
Note on the side: configure the gitignore file properly for your repo as well. For starters, follow the gitignore template to create a proper gitignore file, and then change it the way you need to.
For instance:
Usually, if you use cocoapods, you do not store .workspace folder and its contents in the repo. Instead it's generated using pod install command on each machine that needs it.
It's also common to exclude Pods directory from storing in the repo, although there are pro / cons arguments both ways.

Uploading Files from Laravel App to BitBucket

Here is what im try to achieve, I have a laravel app with a BitBucket repo in my laravel project public directory (public/repo.name).
Now what i want is to run, git add . git commit -m "Changes" and git push from my (public/repo.name) directory inside my laravel app.
I want the git commands to run when i go to lets say, http://127.0.0.1:8000/deploy this would only deploy whatever changes are in (public/repo.name)
Convert your Project to a Git Project
Open your terminal and navigate to the project root directory and run the git init command.
git init
git-init-laravel-project
Git init Laravel project
Git Add and Commit All Files
With the git init command we have successfully converted our project into a local git project. Although we have not yet added a git remote to our project.
As a second step lets go ahead and add and commit all the files to our local git repository.
Run the git add and git commit command on the project root.
git add .
git commit -m "First Commit"
The git add command adds a change in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit.
The git commit command commits the staged snapshot to the project history. Committed snapshots can be thought of as “safe” versions of a project—Git will never change them unless you explicitly ask it to.
Git add and Git commit on Laravel project
Create a New BitBucket Repository
We are now ready to push our changes to a remote repository. But before that we have to create a repository where we can push our changes.
Login to Bitbucket if you already have an account or SignUp for a new account. On the left hand menu , Click on the + symbol to get the option of Creating a New Repository.
Create Bitbucket empty Repo for Laravel.
Enter a suitable project name and click on Create Repository. A blank repository will be created and you will be redirected to the overview page of the repo.
Now we can go ahead and make this repo our remote repo for the Laravel project.
Add Remote Repo and Push Changes
To add the repository you just created as the remote repo for your local Laravel project. Run the following command on your project root in terminal.
git remote add origin https://<repo_owner>#bitbucket.org/<accountname>/<reponame>.git
Replace the repo_owner and accountname in the URL accordingly.
git-laravel-remote-add
git remote add laravel
Now we can go ahead and push the changes to remote repository.
git push -u origin master
git-laravel-push-bitbucket
Git push Laravel project
If you see at source in your bitbucket repository. You should see your Laravel directory structure along with the files.

Push current code to existing GitHub repository

I have several Visual Studio solutions that have both a local repository and one on GitHub. I've already made many changes and successfully pushed those changes to GitHub.
But now Visual Studio has forgotten that one of my local repositories is associated with a GitHub repository and I can't seem to figure out how to reconnect it. In fact, it no longer lists that repository in my list of GitHub repositories.
In the image below, you can see I have a local repository called Toxic, but that repository does not appear in the list of GitHub repositories. If I try publishing the Toxic project to GitHub, it just tells me the repository already exists.
How the heck can I get all of my existing Github repositories to show up in the top section shown above so I can push my latest changes?
it appears the only option is to clone the GitHub repository locally, copy my modified files over the newly created repository, and then check in my changes.
Try fist:
installing Git for Windows (command-line)
cloning your remote repo in a new folder
adding your existing repository as a remote
fetching and see if you can cherry-pick your commits
That is:
git clone https://github.com/<user>/<repo> newFolder
cd newFolder
git remote add old ../path/to/old/local/repo
git fetch old
git log old/master
git cherry-pick old-sha1..old/master
(with old-sha1 being the first commit you want back)
Then add the newFolder in your Visual Studio workspace, and see if everything works (do a modification, add, commit and push)
Unless I'm missing something, it appears the only option is to clone the GitHub repository locally, copy my modified files over the newly created repository, and then check in my changes.
Of course, I lose all my comments and iterations since the last check in to GitHub. And care had to be taken not to delete the .git folder, and to copy over all changed file and delete any that had been removed. Seems like there should be an easier way but this certainly did the trick.
I'm no git expert, but I think I might be able to help, if I'm not too late.
Run:
git remote -v
This should print something in the form of:
origin <remote_repo_url> (fetch)
origin <remote_repo_url> (push)
If you only see:
origin (fetch)
origin (push)
try running:
git remote set-url origin <remote_repo_url>
If you get no output then run:
git remote add origin <remote_repo_url>
And then try
git push -u origin
The -u or --set-upstream flag will set origin as the default repo for your branches.

Create git repository for existing project?

After a close call with deleting a swift file, I noticed that I did not have create the project with it to create a git repository. Is there a way to have an existing project start a git repository? Or do I have to start a new project and move all of my code/files over?
Yes, you can initiate a git repo for an existing project.
There are several ways to initiate and manage a Git repository over an existing project.
Preparations.
There are some files that are not subject to source control. Those are project files from your IDE, compiled classes and so on. To exclude them, get a .gitignore file and put it to the root directory of your project.
Creating a Git repo with command-line tool
go inside the project folder and create new git repository using:
cd path/to/your/project
git init
Then add your files
git add *
and then commit
git commit -am "Initial commit"
if you need to push it to GitHub/BitBucket use
git remote add origin [repository URL]
and then
git push origin master
Creating a Git repo with a gui-based tool or IDE.
You can as well use any gui-based tool. E.g., in IntelliJ IDEA use the menu [VCS] - [Import into version control] - [Create Git repository].
If you are going to use GitHub, there's a convenient GitHub client.

Git Push To New Repo Pushing Wrong Content

I tried creating a new repo on Bitbucket and pushing all of my code to it, but for some reason it is pushing another folder's contents to the repo? I used git status and saw that there were many other files that were untracked yet completely irrelevant.
Things that I done so far -
I have an existing Xcode project
I cd into the folder
I add my origin remote
I git push -u origin --all
I go to Bitbucket and see that another folder of mine has been pushed up
If I use the command ls in my directory, I see that only the files I need are there.
Turns out I hadn't initialized my git repository within that folder....
I used git init and it worked!

Resources