Creating Git Repository for existing project & Git branching.
There were many obstacles when I first started my Software Engineering bootcamp at Flatiron. One of the challenges I faced was learning how to create a git repository and then branching off to another branch on a project I already created on my local environment. The whole concept, at first was weird to me since i’m new to the industry. But after a few trial and errors, I learned how to create a repository and a branch.
When I started my first project I created the project within my local environment without setting up a repository. Which wasn’t letting my partner do a git clone and git pull on the project so they can work on it as well . And I would receive an error whenever I tried to do git push:
Creating a new git Repository for an existing project
Step 1 : Go to your github account and click ‘your repositories’ on the top right corner tab. then click on the ‘New’ tab to get started.
Step 2 : Once in the new page, enter your preferred repository name. you’ll have other options like public or private. you will also be asked if you want to create a README ( instructions), .gitignore(for files you don’t want to be tracked), and licensing. But they’re all optional.
Step 3 : After creating the repository copy the link that is provided.
Step 4 : Go to your terminal and once in the project, set the new remote repository by running the step 4 code below. But if you run into an error when trying to set up a new remote url and it does not work, then follow step 4.5.
Step 4 code:git remote set-url origin https://github.com/akrama23/newproject.gitgit push -u origin main
Step 4.5: If you run across an issue which does not let you set the remote url, then you might already have an existing .git file without knowing when you first created the project on your local machine. Now in this situation you will need to forcibly remove the old remote url/ .git file. To check if you have a remote set already run code bellow
step 4.5:git remote -v
The code above will let you check to see if you have a remote url already set and if you do we can delete it and the add our new remote url to get connected to github from your local machine.
Now if you did have a remote url (.git file) set already and its not the one you want to be connected, do not worry we can get rid of it for good by running the code below which will remove the .git file and will enable use to set a new remote url.
rm -rf .git
This command will forcibly remove the .git file, then you can set a new one by following step 4.
Step 5: Run ‘git push -u origin main’ and set to your github repository that you created before.
Now that we connected to a remote repository we can do a ‘git push’ on our code and add collaborators so then they can work on the project on their time on a different branch with out messing up the original project code.
Why Create a branch ?
- You and your partner can work on the project at the same time
- Will not change the original code unless you merge the branch
- Won’t create merge conflicts if you try to pull new code
Creating a Branch
Step 1 : Create a branch and check the branches. ‘git checkout -b <name>’ lets you create and go right into the new branch your created. ‘git branch’ let’s you see what branch you are currently in and shows other branches.
git checkout -b another_branch
Step 2 : After you’re done adding code in the new branch and want to push to the branch to open later, you can. Then in the master branch you can merge the branch.
git add .
git commit -m "added methods"
Step 3 : After the add and commit, you can push the changes and checkout of the branch into the master branch by ‘git checkout’ then merge the master file with the branch you want to merge with.
git checkout master
git merge another_branch
Step 4: After merging in the master branch. You can add, commit, and push so then the collaborators can git pull on all of the new code and make branches of their own.
Step 5: Once you are done with the branch you created and are finished merging with master branch. It is good practice to delete old unused branches. You can delete old branches after you checkout into the master branch then add the ‘git branch -d branch_name”, the -d stands for delete and you follow that with the branch name.
git branch -d branch_name
Congratulations, now you’re going to be mastering your GitHub and git branching skills effectively.