How to connect local folder to Git repository and start making changes on branches?
Asked Answered
L

2

68

I'm new to source control; in the past, I've manually backed up copies of files and made changes on clones then transferred changes manually to master files once debugged. I realize this is similar to how branches work with Git repositories, however I've never used one.

I downloaded Git and made an account on GitLab, and started a new project. My site is hosted on a local server and my files are saved locally. How do I connect these files to a Git repository and continue developing with branches?

Lekishalela answered 21/3, 2016 at 13:56 Comment(0)
N
149

To register a project as a local Git repository the first thing you need to do is perform the following command at your project root:

git init

This will create a .git folder at your project root and will allow you to start using Git in that repository.


If you want to "push" your local Git repository to a remote Git server (in your case, to GitLab), you'll need to perform the following command first:

git remote add origin <Repository_Location>

You can call origin whatever you like, really, but origin is the standard name for Git remote repositories. <Repository_Location> is the URL to your remote repository. For example, if I had a new project called MyNewProject that I wanted to push to GitLab, I'd perform:

git remote add origin https://gitlab.com/Harmelodic/MyNewProject.git

You can then "push" your changes from your local machine to your remote repo using the following command:

git push origin <branch_name>

where branch name is the name of the branch you want to push, e.g. master.


You can find a good beginners guide to Git here.

Nuggar answered 21/3, 2016 at 14:22 Comment(5)
Thanks for the info! So my site is still hosted on my local server; so I can create a branch remote and it'll add it locally and vice versa? Do I have to create two sites (clones of each other, to do live testing)? Does the branch connect to the live site?Lekishalela
So If I have a script that builds the same files in the repo, I can use this to commit changes without having to clone the repo first?Mervin
I am trying to push a new branch however running the last command results in error: src refspec develop does not match any. error: failed to push some refs to [...]Bainbrudge
I had to commit a file and also do git branch <branch> firstBainbrudge
error: src refspec main does not match anyArgumentation
S
1

Idk if necessary, but I've always named my local folder the same as the remote repo.

Also, prior to a git push, I try to:

git pull origin master

in order to get the latest code in the repository. Then I can port in my changes and commit.

Total text from GitHub:

echo "# xyz_repo" >> README.md

git init

git add README.md

git commit -m "first commit"

git branch -M main

git remote add origin https://github.com/mikeyj777/Coursera_DataStructsAndAlgos.git

git push -u origin main
Sixfold answered 12/10, 2020 at 0:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.