Creating a branch in a repo
We shall be looking at how to work with some advanced git commands.
When collaborating on a project using github, and you want to work on an aspect of the project, say for example, the login page, it is best to create a branch in the repo.
How do you create a branch? A branch is simply a version of a repo. The project working directory is the repo. Branches are created in the directory. Creating a branch is to enable the independent parts that make up a project to be developed separately and then merged together using through pull request to the project repo ( default directory).
The default directory is the repo that houses the project, called main. The branch is like a sub directory (a tree) which is created and attached to the main.
So follow these steps to create a branch in a repo
- open git
- If the default root directory is master, switch it to main by running
git branch -m master main
- Then navigate to
cd desktop
- navigate to the folder containing the files for your new branch by running
cd folder name
- Initialize the git by running this command
git init
- Run this command to initialize the branching process
git branch
- Then create the branch name
for example
git branch Login-Page
- The branch name will be created. Also run,
git branch
- Run this command to see the list of branches
git branch --list
Note: the current working branch usually carries a
* sign
- Run this command to add the new branch to the main
git branch -a
- Run this command to switch to the new branch
**You would notice that the directory has changed from main to Login-Pagegit checkout Login-Page
- To add file to this new branch, run this command
git add .
- commit your file
git commit -m "new commit"
- On github, copy the remote branch URL and run this command
git remote add origin URL
Note: Go to the repo on github, then copy the URL on the address bar
- When done, run this command
git push origin Login-Page
Voila, your files has been pushed to the new branch
Please like or comment and share