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 run:
git remote set-url origin https://github.com/akrama23/newproject.git
Step 5: Run ‘git push’ 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.
Congratulations, now you’re going to be mastering your git branching skills effectively.