2.2) Learn Git and GitHub
Let me start by introducing you to Git and GitHub :
What is Git?
Git is a free and open source distributed version control system designed to handle projects with speed and efficiency.
- It basically is a tool to manage your source code revisions.
- Code can be stored in a remote place, so physical damage to local storage is not a issue of concern.
- Ability to developers to work from remote places and gives ability to collaborate with each other.
- Many developers contributed to Git to make it more useful and reliable.
What is GitHub?
GitHub is a web-based Git repository hosting service
GitHub offers all of the distributed revision control and source code management (S.C.M.) functionality of Git as well as adding its own features.
Now you may ask, why should you use GitHub. Well, there are plenty of reasons :
- Have your code reviewed by the community.
- Collaborate and track changes in your code across versions.
- GitHub can integrate with common platforms such as Amazon and Google Cloud, services such as Code Climate to track your feedback, and can highlight syntax in over 200 different programming languages.
Basic terminologies used in GitHub can be referred at : GitHub Glossary
The basic model of git is as follows:
How to install Git?
Install Git on Mac OS X / Windows / Linux
Once you install git you might want to check if it is installed properly. Simply run the following command in the terminal :
git — version
You need to specify your username and email using the following commands:
git config — global user.name “Vinit”git config — global user.email “youremail@gmail.com”
Here, global refers to the current user.
You can check you configuration details using :
git config — list
To be frank, git can be a bit overwhelming for some users. But don’t worry, git provides help for its various commands which can be accessed by :
git help commandEg: git help commit
To start working with git, you first need to initialise it in the current directory. For this purpose, you may use the following command :
git init
For adding all files to staging area, you can use the command :
git add .
Similarly, you can add individual files.
Eg: git add index.html
For making a commit to repository, use the command :
git commit -m “Message”
Here, ‘-m’ is used to specify the message.
Suppose you wish to view all commits that you have made to your repository. Just execute the following command :
git log
Probably, the most important command in git which compares git repository with working project and checks for any changes.
git status
To check the differences between local copy and repository file :
git diff
To check differences between staging area file and repository :
git diff — staged
Suppose you want to delete a local file, just execute the command :
git rm filenameEg: git rm index.html
Make sure you commit your changes to delete the file from repository.
In certain cases, you may want rename a file. For this purpose just run the command :
Eg: git mv index.html home.html
For removing a file from staging area, just execute the command :
git reset HEAD index.html
Now, let us move on to some GitHub commands.
git remote add nickname_of_repo URL_of_repoEg : git remote add newrepo https://github.com/username/reponame.git
For checking your remote repository name :
git remote
To push the commit changes to remote repository :
git push -u repo_nickname masterEg : git push -u newrepo master
To pull the changes from remote repository branch to local copy, use the command :
git pull repo_name masterEg: git pull origin master
If you want to get a copy of an existing Git repository — for example, a project you’d like to contribute to, use the command :
git clone https://github.com/username/reponame.git
You may have seen that every time we make a ‘git push’, we are prompted to enter our username and password. Instead we could use a SSH key for authentication.
1. Generate an SSH key
Linux/Mac
Open terminal to create ssh keys:
cd ~ #Your home directory
ssh-keygen -t rsa #Press enter for all values
For Windows
(Only works if the commit program is capable of using certificates/private & public ssh keys)
- Use Putty Gen to generate a key
- Export the key as an open SSH key
Here is a walkthrough on putty gen for the above steps
2. Associate the SSH key with the remote repository
This step varies, depending on how your remote is set up.
- If it is a GitHub repository and you have administrative privileges, go to settings and click ‘add SSH key’. Copy the contents of your
~/.ssh/id_rsa.pub
into the field labeled 'Key'. - If your repository is administered by somebody else, give the administrator your
id_rsa.pub
.
3. Set your remote URL to a form that supports SSH 1
If you have done the steps above and are still getting the password prompt, make sure your repo URL is in the form
git+ssh://git@github.com/username/reponame.git
as opposed to
https://github.com/username/reponame.git
To see your repo URL, run:
git remote show origin
You can change the URL with:
git remote set-url origin git+ssh://git@github.com/username/reponame.git
The best place to learn git and GitHub is thenewboston Github Playlist.
That’s all for now, in the next post, we will see how to setup Hasura Local Development.
-Vinit Neogi and Akash Dabhi