In my previous article, I covered a brief introduction to Git and GitHub. Have a look here if you haven't yet:
My goal for this article is to cover how you can easily get started with Git & GitHub, how to install it, and some basic/IMP commands. So, let's start!
- Setting up the environment (Git, VS code installation, GitHub account creation, SSH key generation etc.)
- Basic Commands
- Cloning GitHub Repo on your local machine
- Creating Repo locally and initializing on GitHub
- Undoing in Git
- Forking in GitHub
- Code editor – I prefer VS Code
- Git installation on your system
- GitHub account
- SSH keys for authentication
Using the SSH protocol, you can connect and authenticate to remote servers and services. With SSH keys, you can connect to GitHub without supplying your username and personal access token at each visit.
After installing Git on your system and Creating GitHub account, to connect to your GitHub account from your local machine via SSH you need to generating a new ssh key for the authentication. Follow the below documentation for detail overview on creating and adding SSH keys.
When you set up SSH, you will need to generate a new SSH key and add it to the ssh-agent. It will generate two keys, one public and one private. You must add the public SSH key to your account on GitHub before you use the key to authenticate.
Follow the below documentation for the process of Adding public SSH key to your GitHub account
Now that everything is set up, let's look at some common Git commands:
git Clone: Bring a repository that is hosted somewhere like github into a folder on your local machine
git add:Track your files and changes in git
git commit:Save your files in Git
git pull:Download changes from remote repo to your local machine, the opposite to push
git add .To track all unsaved files and changes for github
git commit -m "title" -m "description"
git push:Upload git commits to a remote repo, like github
It's similar to downloading or cloning someone else's open source project and updating/modifying it or even working with your own online repository.
- In Visual Studio code, open an empty folder and from the top of that folder open a terminal window
- Clone with SSH that we just set up
Run the following command:
Git clone firstname.lastname@example.org:WardaLiaqat01/Test\-Repo.git(Repo will be cloned with all files and folders)
- You can view all hidden files using
- Make some changes to the file and run
git statuscommand, it will show you all modified and untracked created files to be saved and commit.
git add .will save all new changes.
git commit -m“message you want to add with commit” -m “add description of commit” will save your changes locally.
git push origin masterwill add update your changes on your GitHub Repo
Due to the recent "Replacing master with main in GitHub" action, you may notice that there is a
refs/heads/main. As a result, the following command may change from
git push origin HEAD:master to
git push origin HEAD:main
You can try
git push origin HEAD:master as a more local-reference-independent solution. This explicitly states that you want to push the local ref HEAD to the remote ref master.
- After successful push you can see your all code changes are live on GitHub.
You can also see all the commits
Here are all the changes, green means it was added and red means it was removed. It's quite handy to be able to revert back to the previous version in case you don't like the changes.
Essentially, it is just as if you were creating your development project locally and then uploading it to Git
- Create any Repo/Project locally in VS code
- It won’t be a part of GitHub Repo as for now
- After adding files your project and updating them we need to initialize our Repo so Git can recognize it
git init(To initialize local git Repo)
- After your Repo is initialized then you can follow all the steps that we performed above (including status, add, commit etc.)
git push origin master:Here this command will through you an error
It’s because we didn’t clone this down from GitHub repository. We didn’t create this Repo on GitHub, we created it locally. So git is saying
I don’t have any idea where to push this because it doesn’t connect to anything. So, we need to create that connection.
The easiest way is to do it from GitHub online interface
- Create an empty GitHub repository with name and description and as a public repo
git remote add origin [repo SSH/clone link]We are using this to add a reference to remote repo on GitHub
git remote -vwill show all the remote repos
git push -u origin HEAD:main(-u for upstream, that means I can push simply by "git push" command in future)
Undoing can be quite helpful. In some cases, if you made some changes mistakenly and want to revert them, for example if you want to revert a commit, the git reset command can prove very helpful.
git reset :To undo add/stage changes.
git reset HEAD~1 :To undo commit (will undo the last/most recent commit)
git log :If we want yo undo any previous commit then this command will help
git reset (hash of that commit) :You will get this from logs but it won't remove data
git reset --hard (hash of that commit) :It will remove the code as well
In a nutshell, forking means copying someone else's code into your own local repository to make updates and then create a pull request to the original repository after successful changes or create a pull request in your own local repository.
As a programmer, you'll find Git and GitHub quite useful. Thus, we learned how to get started using Git & GitHub with a few basic commands. I hope you have found this article helpful in some way.
If you have any comments, let me know.
if you would like to explore some more handy Git commands then visit the below URL.