Git is a collaborative tool used by developers for tracking changes in the source code. It is crucial for version control and coordinating work among the team of developers.
The article aims to guide the user through a step-by-step process of making a new feature change without affecting the code committed to the master branch of the remote repository.
A remote repository is a common repository used by the development team, hosted on an internal server or code hosting service e.g. Bitbucket, GitLab, or GitHub.
Clone a Repository
To create a copy of the remote repository locally on your PC, use the below command. Cloning a repository allows you to have a copy of the remote repository. It also allows the user to have a backup copy of the repository should there be an internet downtime affecting access to the remote repository.
#To clone the remote repository locally #for example $ git clone https://github.com/MaureenBarasa/test.git
Create a New Branch
A git branch allows the user to create their own working directory where one can make changes without affecting the main source code. It creates an independent directory that a developer can use for development purposes without having to worry about messing up the original code. The below code snippet explains how to create branches using git commands.
#check the available branches on the local repository $ git branch #create a new branch called newfeature on your local repository $ git branch newfeature #confirm the new branch has been created $ git branch #switch from master branch to the new branch created $ git checkout newfeature
alternatively we can use the command
#to create a new branch called newfeature and switch from master to the new branch $ git checkout -b newfeature
Add New Files to the New Branch and Commit
Once you have switched to the new branch, you can make modifications to the code. Add files if you need to e. t. c. For git to see and track the modifications made on your code, we need to stage the changes made. We can do this using the git add command. To save the changes made to our local repository, we use the git commit command. See the below code snippet.
#to check all our untracked files and changes $ git status #to track all changes made $ git add . #to track only a single file $ git add test.txt #to save changes made to the local repository $ git commit -m "commit message"
Now that all our changes are saved, we are now ready to push these changes to the remote repository. See next topic.
Push Changes to the Remote Repository
To push changes made on our local repository to our remote repository, use the below commands on the code snippet.
#to confirm the url of your remote repository $ git remote -v #to create a newfeature branch on remote repository and push changes to that branch $ git push origin newfeature
You have now successfully managed to create a feature branch and made changes to your code without affecting the master branch. Subsequently, the user may want to add the changes made to the master branch. To do this, the user has to create a merge/pull request as discussed in the next topic.
Create a Merge/Pull Request from GitHub
Once the changes to your local repository on the newfeature branch have been pushed to the remote repository new feature branch, we need to create a pull request that has to be approved before we can merge changes made on the feature branch to the master.
Log in to your github account and go to your repository, Click on pull requests as shown below. Then, select new pull request.
On the window that opens select the branches, you want to merge. For example, in our case, we want to merge newfeature branch to the master branch. Then click on create a pull request.
You can then assign a person to approve the pull request. And choose to send a notification to them. Once the assignee, gets the notification, they can go through your code and if all is ok, they can confirm the merge of the two branches as per the below image.
Once a merge has been successfully done, The assignee can now delete the newfeature branch. You now have successfully merged the changes made to the newfeature branch to the master branch.
Delete the Feature Branch on your Local Repository
To delete the newfeature branch created on your local repository, use the below command.
$ git branch -d newfeature