Version control is an important concept in software development. Collaboration of many contributors to one project requires great coordination. Sometimes, experimental changes to the project may be needed, and if two or more people made conflicting changes, it is very time consuming to resolve the issue, especially if the collaborators are working over long distances or if direct communication is impractical. Version control is a piece of software that aids with resolving those issues and helps increasing the speed and efficiency of development by keeping track of file changes and archiving older versions of a project.
Git is a free and open source version control software (among others like SVN, Mecurial, Monotone and many more…) aimed to expedite IT projects of both simple and complex architecture[1]. GitHub is an online repository (project) hosting website designed to work with Git. As of 2016, Git and GitHub are two of the most popular tools among professionals working with software development[2].
In the next 9 steps, we will learn how to install Git, set up a GitHub account and start using them for our projects. You should be familiar with navigating between different folders using your operating system command shell. Tutorial on how to use the command line can be found here:
$ git config --global user.name "your-github-username"
$ git config --global email "your-email@domain.com"
$ git clone paste the copied https link here
Or
Username for 'https://github.com': yourusername
Password for 'https://yourusername@github.com': yourpassword
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
$ git init
$ git add .
# Add all the files and folders in the current folder.
# Alternatively, you can use git add filename for individual
# files.
$ git commit -m “Initial commit”
$ git remote add origin paste the https link in step 3 here
# Sets the new remote
$ git remote -v
# Verifies the new remote URL
$ git push origin main
For software development projects, there are many text editors that support Git version control. The most popular ones are Visual Studio Code, Atom, and Sublime Text. If you are working with a modern integrated development environment (IDE), it will most probably also have Git support. Consult the relevant software documentation for more information on version control support. After installing and configuring your editor, you should be able to start writing your code and building your project. Next comes the exciting part!
To start contributing to the project we are hosting on GitHub, the add, commit, and push commands are our powerful allies. After you have finished with your edits on the local file, it is time to upload it onto the project GitHub repository.
$ git add .
$ git commit -m “description of your changes”
$ git push
For individual files, you can use this command instead:
$ git add filename
Conflict happens when multiple people are working on one file and their local changes does not match that of the online repository. In this case, attempts to push your changes to the repository will result in an error message:
To https://github.com/yourusername/repo.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'https://github.com/ yourusername/repo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Use the pull command to download the conflicting changes from the repository.
$ git pull
Username for 'https://github.com': yourusername
Password for 'https://miketvo@github.com':
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/yourusername/repo
7b32407..050181c main -> origin/main
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
When working on a new feature of a project, it is a good idea to modify a copy of the project itself before “merging” the copy into the main project files. This is called “branching” and is an essential process of software development.
Navigate to our git-ready project folder using the command line and type the following command:
$ git branch
* main # The currently active branch
branch1
branch2
The branch command can also create new branches:
$ git branch new-branch # This command has no output
To switch between branches, we “check them out”:
$ git checkout new-branch
Switched to branch 'new-branch'
When we are done with implementing new files into our branch, it is time “push” it to GitHub. If the branch already exists on GitHub and we are just adding to it, simply follow Step 6. On the other hand, if it only exists on our local machine and not on GitHub, we will have to use these commands instead:
$ git add .
$ git commit -m “add a new branch”
$ git push origin new-branch
This will create a new branch in the online GitHub repository and pushes the content of our new branch into it. After development of a new feature is complete, we can go to our GitHub repository with a web browser to create a pull request to merge the new feature into the main branch. More information on branching and merging can be found in the GitHub’s Hello World tutorial or here
This tutorial is intended as a crash course to get you started with using Git and GitHub. More advanced tutorials such as these on freeCodeCamp can also help us delve deeper into software development workflow using version control. Git is a very powerful and versatile tool, so to utilize all of its feature, please refer to the Git and GitHub documentation for more comprehensive information. In the IT world, it is unavoidable that we will have to read a lot of documentation as new technologies emerge and replace the obsolete. This is one of the most important skills for an IT professional.
If you have any feedback or improvement on this tutorial, feel free to leave a comment or contact me via email.
Godspeed and good luck on your journey!