Introduction to Git and Github
Beginners guide to understanding and getting started with Git and Github.
Introduction
Git is a technology that lets you maintain the history of the project by version control. Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later.
It allows you to revert selected files back to a previous state, revert the entire project back to a previous state, compare changes over time, see who last modified something that might be causing a problem, who introduced an issue, and when, and more. Using a VCS also generally means that if you screw things up or lose files, you can easily recover. In addition, you get all this for very little overhead.
What is Git
Git is a distributed version control system, using Git you can clone the repository which is a full backup of all the data, including its full history. This technology enables millions of developers to contribute to the same project at the same time!
Git is an open-source technology created by Linus Torvalds. It is arguably the most influential developer tool created in my opinion.
What is Github
Github is the single largest host for Git repositories and is the central point of collaboration for millions of developers and projects. A large percentage of all Git repositories are hosted on GitHub, and many open-source projects use it for Git hosting, issue tracking, code review, and other things.
Repository - It is a place that holds your code.
Github is owned by Microsoft. Similar platforms to Github are Bitbucket, SourceForge, and many more.
Setting up Git and Github
Git
You need to download git for your system using the official download page. It is available for macOS, Windows, and Linux/Unix.
If you want you can download the GUI Client for Git, but in this blog, we will be going through the Git Command line to set up and push your repositories.
Github
You can signup for Github here, it is completely free.
Setup your account, select a username that has not already been taken and you are good to proceed!
Create a new repository on GitHub, you will see something like this below.
Git basics
Create
Using Git you can push an existing repository that exists on your local system onto Github through the command line using the following commands.
//git remote add origin "URL"
git remote add origin https://github.com/Eshan-Sharma/Demo-Repository.git
git branch -M main
git push -u origin main
If you don't have any repository on the local system you can create a new local repository using,
git init
Local changes
Once your folder is created. You can start writing your code/modifying your code.
Commands
To track changes in your working directory, use
git status
To see the changes to tracked file use,
git diff
To add all changes made on your working repository to the staging area use,
git add .
To add some changes in to the next commit use,
git add -p <file>
Commit all local changes in tracked files to the main repository from the staging area. use,
//-m is used to add a message with the commit.
git commit -a -m "Committing changes in the file."
Branching and Tags
Git's branching model is its "Killer feature" and sets Git apart in the VCS community. It is incredibly lightweight, making branching operations nearly instantaneous, and switching back and forth between branches is generally just as fast. Git encourages workflows that branch and merge often, even multiple times in a day. Understanding and mastering this feature gives you a powerful and unique tool and can entirely change the way that you develop.
Understanding Branches
When you make a commit, Git stores a commit object that contains a pointer to the snapshot of the content you staged. This object also contains the author’s name and email address, the message that you typed, and pointers to the commit or commits that directly came before this commit (its parent or parents): zero parents for the initial commit, one parent for a normal commit, and multiple parents for a commit that results from a merge of two or more branches.
Visualization of Git branch is just like the name suggests, imagine a tree with multiple branches. The tree is the main repository or main branch. Learn more using infographics here.
Commands
To list all existing branches use,
git branch
To switch HEAD branch use,
git checkout <branch>
To create a new branch based on your current HEAD use,
git branch <new-branch>
Create a new tracking branch based on a remote branch use,
git checkout --track <remote/branch>
Delete a local branch use,
git branch -d <branch>
Mark the current commit with a tag use,
git tag <tag-name>
Update and Publish
Commands
List all currently configured remotes use,
$ git remote -v
Show information about a remote use,
$ git remote show <remote>
Add a new remote repository, named use,
$ git remote add <remote> <url>
Download all changes from , but don‘t integrate them into HEAD use,
$ git fetch <remote>
Download changes and directly merge/integrate into HEAD use,
$ git pull <remote> <branch>
Publish local changes on a remote use,
$ git push <remote> <branch>
Delete a branch on the remote use,
$ git branch -dr <remote/branch>
Publish your tags use,
$ git push --tags
Merge and Rebase
Commands
Merge into your current HEAD use,
$ git merge <branch>
Rebase your current HEAD onto Don‘t rebase published commits! use,
$ git rebase <branch>
Abort a rebase use,
$ git rebase --abort
Continue a rebase after resolving conflicts use,
$ git rebase --continue
Use your configured merge tool to solve conflicts use,
$ git mergetool
Use your editor to manually solve conflicts use,
$ git add <resolved-file>
After resolving mark the file as resolved use,
$ git rm <resolved-file>
Undo
Commands
Discard all local changes in your working directory use,
$ git reset --hard HEAD
Discard local changes in a specific file use,
$ git checkout <file>
Revert a commit (by producing a new commit with contrary changes) use,
$ git revert <commit>
Reset your HEAD pointer to a previous commit & discard all changes since then use,
$ git reset --hard <commit>
Reset your HEAD pointer to a previous commit & preserve all changes as unstaged changes use,
$ git reset <commit>
Reset your HEAD pointer to a previous commit & preserve uncommitted local changes use,
$ git reset --keep <commit>
Working with existing projects
Create a fork of the project, this will create your local copy of the main branch.
The URL from where you have forked the repository is called as Upstream URL.
Read the README file, set up the project according to the instructions provided, and get familiar with the project.
If you encounter any bugs or want to contribute to a feature, create a separate branch for each bug/feature.
- Only one pull request can be raised per branch, if you combine all the bugs you are working on in one pull request it would be extremely difficult for the code maintainer to accept your pull request.
Conclusion
Now, you are familiar with the git command and Github. My suggestion to you would be to practice, make your project, and maintain a repository of it on Github.
Or connect with an open-source project, Clone the repo, understand and use the project for a couple of days then try to contribute to the project. Make sure to always make a new branch for each feature/bug you work on.
All the best!