DEV Community

G Sudarshan
G Sudarshan

Posted on

Git and GitHub 101

Git is the open source distributed version control system that facilitates GitHub activities on your laptop or desktop. This blog post summarizes commonly used Git command line instructions for quick reference.

Install

Git for All Platforms
http://git-scm.com
Git distributions for Linux and POSIX systems are also available on the official Git SCM web site

Configure tooling ( you have to configure this only one time after installation )

Configure user information for all local repositories

$ git config --global user.name "[name]"
Sets the name you want attached to your commit transactions

$ git config --global user.email "[email address]"
Sets the email you want attached to your commit transactions

$ git config --global color.ui auto
Enables helpful colorization of command line output ( you can skip this if you want )

Create repositories

When starting out with a new repository, you only need to do it once; either locally, then push to GitHub, or by cloning an existing repository.

$ git init
Turn an existing directory into a git repository

$ git clone [url]
Clone (download) a repository that already exists on
GitHub, including all of the files, branches, and commits.

connecting Git and GitHub via SSH

This is one of the most important things to set-up git and GitHub. I'll recommend you to follow these two pages of official GitHub documentation to genarate and add SSH keys.
Generating a new SSH key and adding it to the ssh-agent
Adding a new SSH key to your GitHub account

Synchronize changes ( Most frequently used part )

Synchronize your local repository with the remote repository
on GitHub.com

$ git add [file]
To add file to stage. Mostly you will be using git add . to add all file at once

$ git commit -m "commit message"
All the file changes that are on the stage will be commited ( saved ) in local git repo after this command.

$ git status
To check current status of repository.

Adding a remote repository

To add a new remote, use the git remote add command on the terminal, in the directory your repository is stored at.

The git remote add command takes two arguments:

A remote name, for example, origin
A remote URL, for example, https://github.com/user/repo.git

$ git remote add origin https://github.com/user/repo.git
# Set a new remote

$ git remote -v
# Verify new remote
> origin  https://github.com/user/repo.git (fetch)
> origin  https://github.com/user/repo.git (push)
Enter fullscreen mode Exit fullscreen mode

Remote repository synchronization

to push the changes to remote repo for first time use the following command
$ git push [remote] [branch] -u
✓ Remember -u is used only when pushing for first time later on you can simple use
$ git push [remote] [branch]
Uploads all local branch commits to GitHub

$ git fetch [remote] [branch]
Downloads all history from the remote tracking branches

$ git merge
Combines remote tracking branch into current local branch

$ git pull [remote] [branch]
Updates your current local working branch with all new
commits from the corresponding remote branch on GitHub.
git pull is a combination of git fetch and git merge

The .gitignore file

Sometimes it may be a good idea to exclude files from being
tracked with Git. This is typically done in a special file named .gitignore . You can find helpful templates for .gitignore files at github.com/github/gitignore.

Branches

Branches are an important part of working with Git. Any
commits you make will be made on the branch you're currently
“checked out” to. Use git status to see which branch that is.

$ git branch [branch-name]
Creates a new branch

$ git checkout [branch-name]
Switches to the specified branch and updates the
working directory

$ git merge [branch]
Combines the specified branch’s history into the current branch. This is usually done in pull requests, but is an important Git operation.

$ git branch -d [branch-name]
Deletes the specified branch


Glossary

git: an open source, distributed version-control system
GitHub: a platform for hosting and collaborating on Git repositories
commit: a Git object, a snapshot of your entire repository compressed into a SHA
branch: a lightweight movable pointer to a commit
clone: a local version of a repository, including all commits and branches
remote: a common repository on GitHub that all team member use to exchange their changes
fork: a copy of a repository on GitHub owned by a different user
pull request: a place to compare and discuss the differences introduced on a branch with reviews, comments, integrated
tests, and more
HEAD: representing your current working directory, the HEAD pointer can be moved to different branches, tags, or commits
when using git checkout

Learning Resources

  1. Git branching interactive learning
  2. Git and GitHub for Beginners - Crash Course

Thank You!

🐦 @g_sudarshan11 Twitter

🐧 G-Sudarshan GitHub

🏒 g-sudarshan LinkedIn

📸 @g_sudarshan11 Instagram

Top comments (0)