How to use git to upload code to GitHub?
What is Git?
Git is a version control system, used to version your code and keep track of its changes.
Install GIT by following the instructions
Why Version Control?
Let’s say you have started writing a simple code and you share it with your friend. Your friend on looking at the code gets hit with an idea to add a functionality. So, you send him your code and he start working on it. On the background you too are fiddling with the code with a different approach. Two days later when you meet with your friend, both of your code is now complex to integrate. This is where version control comes in.
So how does it work?
Let’s term the first code you had created to be the base code (or Version 0.0.0)
Now on each change you would be updating the numbers.
Changes can be classified into
- Minor bug fix
- Major bug fix
- Release / Deployed
So, remember our software is in 0.0.0 Version
Once you fixed all the minor bugs, you can increase the third digit on the version number. i.e. 0.0.0 will become 0.0.1
When you have fixed major bugs, you can increase the second digit on the version number, i.e. 0.0.1 will become 0.2.1
And if your code is ready to be release / shared or deployed you can increase the third digit on the version number, i.e. 1.3.2
Once you can understand this part, here is the method which we use in the real world.
The third digit would be used for fixing bugs.
The second digit would be for adding new features.
And the first digit would be for Major Changes.
Now that you have understood version, Let’s get back to our code situation.
Welcome to GitHub
GitHub is a place where you can upload your code and have other’s edit or add codes etc.
You can keep your code open source (Anyone can see your code) or you can keep it in a private repository (Only you and people you add can see the code)
Assuming you already have a GitHub account, you need to add ssh to be able to add code via command line. You can follow the tutorial below.
Step 1: Create Repository
Repository is like a folder where all your codes will be residing.
You can create a repository by first creating a folder in your computer.
mkdir my-project #create a folder
and then go inside your project folder.
cd my-project
Existing Repository
If you already have repository, you can clone the repository by first copying the ssh link
And the paste the link with the command
git clone repository_ssh_link
Step 2: Initiate Git project
git init
Step 3: Add Codes
Start adding your codes!
Step 4: Add code to commit
git add .
Adding specific files
If you want to exclude some files and just add selective files you can use -f command
git add -f file1 file2
Step 5: See selected files.
git status
Step 6: Commit
git commit -m "Describe the Changes"
Step 7: Push
git push origin master
And we have done it. But remember your friend waiting for your code? Let’s look into it.
When you first create repository, you will have a master branch. This can be treated the main code.
Now since your friend also wants to contribute, we can create a branch for him so that the main code can exist in peace and remain in unchanged state.
Step 8: Create Branch
git checkout -b Branch_name
If the branch already exists, it will just switch to the branch, if not, it will create a new one
To see which branch is currently active
git checkout
To see all the branches, you can use
git branch
Step 9: Commit on Branch
You can add files and commit by following Step 4 to Step 6.
git push origin Branch_name
And you have successfully added your project to GitHub and brought it under version control. You can see your success by logging into GitHub repository.
Bonus
You can add Version Number to your git repository like this
git tag -a "v1.5.0-beta" -m "version v1.5.0-beta"
Conclusion
By following the nine steps, you can create, commit, push, and branch your code using git commands. You can also tag your code with version numbers to indicate the stage of development.
Share with those in need :)