If you’re new to source code management, kindly note that Git and Github are two different things: Git is the version control system to manage the source code; it’s like a command line utility which we install on our local system to manage different versions / changes of our source code. Whereas Github is an online cloud service provider where we can keep these versions same as our local machine and can also get other developers involved with the code too.
This tutorial covers very basics of Git or serve as a quick reference to basic commands needed for initialization and maintaining simple repositories. So, let’s start quickly and create an account on github.com if not already created. You’ll get your custom URL e.g., github.com/username. Github enforces Two-Factor Authentication (2FA) for security purposes, so be prepared for some extra steps to setup; kindly consult the official documentation of Github for that purpose.
Any new repository (repo) you make at Github gets its own URL like github.com/username/myapp.git; so first, make a new empty repo there and name it as ‘myapp’.
Next, install Git if not already installed on your local system. You can simply enter the following command to install Git on Ubuntu / Debian Linux systems;
sudo apt install git
Then create an app folder named myapp; you may enter following command for that purpose:
mkdir myapp && cd myapp
By executing above command, you’ll also be taken inside this newly created folder myapp. Scaffold myapp with some contents (these could be any type of source code files e.g., a Laravel app, node app, Python app). Next, enter the following commands in the given order (replace ‘username’ with your username):
git init
git branch -m main
git add .
git commit -m "Fresh Install"
git remote add origin https://github.com/username/myapp.git
git push -u origin main
First command will initiate myapp folder as local Git repo. Next couple of commands will add and commit contents of myapp for the first time. Second last command will add remote repo address. And the last command will push the commit named Fresh Install to the Github repo i.e., github.com/username/myapp.git. Just remember that the above commands are for first time only.
Whenever you push to Github, it’ll require user login and password for Github account. So, enter the required credentials each time or you may set Git environment variables by entering following commands to save login and password and avoid entering credentials each time;
git config --global user.name "username"
git config --global user.email "youremail@ggmail.com"
git config --global user.password "yourgeneratedtoken"
git config --global credential.helper store
The next time whenever you want to update the Github repo with your local change / commit, you’d just enter following commands in the given order:
git add .
git commit -m "Another change"
git push origin main
Now what if there are some changes at Github repo (e.g. changes made by other developers in your repo through Change Request process) and you want to update local repo with those changes. For that purpose, just enter the following command in your local repo folder which will update the local repo with the changes made on Github repo;
git pull
There might be some situations where old Git version is being used or the ‘master’ branch is being used as the parent branch instead of ‘main’. In those cases, ‘master’ branch has to be changed into ‘main’ branch. Therefore, execute following commands in your local repo to make ‘main’ as parent branch instead of ‘master’:
git branch -m master main
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main
git push --force origin main
If you get Git’s error “fatal: The current branch has no upstream branch”, then enter following command:
git branch --set-upstream-to=origin/main main