Git Basics

Version Control:

System that records changes done on files over time so that we can recall specific version later.
  1. Local Version Control System
  2. Centralized Version Control System
  3. Distributed Version Control System


Git

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Git Advantages

  1. Free and Open Source
  2. Branching and Merging
    • Frictionless Context Switching
    • Role-Based Codelines
    • Feature Based Workflow
    • Disposable Experimentation
  3. Small and Fast
  4. Distributed
  5. Staging Area


Basic Commands

git help

To get help of specific command.
e.g. $ git help log

git init

Create empty Git repo in specified directory. Run with no arguments to initialize the current directory as a git repository.
e.g. $ git init

git status

List which files are staged, unstaged, and untracked.
e.g $ git status

git clone

Clone repo located at onto local machine. Original repo can be located on the local filesystem or on a remote machine via HTTP or SSH
e.g. $ git clone http://github.com/rahuljadhav/social

git config user.name

Define author name to be used for all commits in current repo.
e.g. git config user.name 'rahuljadhav'

git config user.email

Define email to be used in git current repo.
e.g. git config user.email 'rjadhav@tavisca.com'

git add

Stage all changes in for the next commit. Replace with a to change a specific file.
e.g. $ git add 'Test.txt' $ git add 'Test1.txt' 'Test2.txt'

git commit -m ""

Commit the staged snapshot, but instead of launching a text editor, use as the commit message.
e.g. $ git commit -m "added Test.txt file"

git log

Display the entire commit history using the default format. For customization see additional options.
e.g. $ git log

git diff

Show unstaged changes between your index and working directory.
e.g. $ git diff To see staged changes that will go in next commit e.g. $ git diff --staged or $ git diff --cached


Undoing Changes

git revert

Create new commit that undoes all of the changes made in , then apply it to the current branch.
e.g. $ git revert hash_value

git reset

Remove from the staging area, but leave the working directory unchanged. This unstages a file without overwriting any changes.
e.g. $ git reset file_name

git clean -n

Shows which files would be removed from working directory. Use the -f flag in place of the -n flag to execute the clean.
e.g. $ git clean -n (shows changed files ) $ git clean -f (removed all changed files from working directory )
$ git clean -i (Opens Interactive Mode )


Git Branches

git branch

List all of the branches in your repo. Add a argument to create a new branch with the name .
e.g $ git branch

git branch

Creates branch of specified name in current repository
e.g $ git branch 'branch_name'

git branch -d

Deletes the branch having specified name from current repository
e.g $ git branch 'branch_name'

git checkout -b

Create and check out a new branch named . Drop the -b flag to checkout an existing branch.
e.g. $ git checkout -b Branch_Name

git merge

Merge into the current branch.
e.g. $ git merge 'branch_Name'


Rewriting Git History

git commit --amend

Replace the last commit with the staged changes and last commit combined. Use with nothing staged to edit the last commit’s message.
e.g. $ git commit --amend

git rebase

To check the differences or conflicts between commits performed on different branches. Use the rebase command rather than the merge command.
e.g. $ git rebase master $ git rebase --continue $ git rebase --skip $ git rebase --abort

git reflog

Show a log of changes to the local repository's HEAD. Add --relative-date flag to show date info or --all to show all refs.
e.g $ git reflog --relative-date


Remote Repositories

git remote add

Create a new connection to a remote repo. After adding`` a remote, you can use as a shortcut for in other commands.
e.g. $ git remote add origin "https://github.com/rahuljadhav/project"

git fetch

Fetches a specific , from the repo. Leave off to fetch all remote refs.
e.g. git fetch https://github.com/rahuljadhav/project ProjectBranch

git pull

Fetch the specified remote’s copy of current branch and immediately merge it into the local copy.
e.g. git pull https://github.com/rahuljadhav/project

git push

Push the branch to , along with necessary commits and objects. Creates named branch in the remote repo if it doesn’t exist.
e.g. $ git push https://github.com/rahuljadhav/project ProjectBranch


Tags

git tag

Displays list of tags
e.g. $ git tag

Two types of Tag

  1. Lightweight Tags: A lightweight tag is very much like a branch that doesn’t change – it’s just a pointer to a specific commit.3/3/2015 4:52:24 PM 3/3/2015 4:52:25 PM e.g. git tag v1.4-lw
  2. Annotated Tags: Annotated tags are stored as full objects in the Git database. They’re checksummed; contain the tagger name, e-mail, and date; have a tagging message.e.g. $ git tag -a v1.4 -m 'my version 1.4'

git show

To view tag related data. e.g. $ git show v1.4

Rebase and Merge:

Merge

This creates a new “merge commit” in the feature branch that ties together the histories of both branches, giving you a branch structure.

Rebase

This moves the entire feature branch to begin on the tip of the master branch, effectively incorporating all of the new commits in master. But, instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch.

Git Repository Hosting Services:

  1. Bitbucket
  2. Cloudbase
  3. GitHub
  4. GitLab etc.

Refer following links for more details:

  • https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control
  • http://gitimmersion.com/index.html
  • https://try.github.io/levels/1/challenges/1
  • http://gitreal.codeschool.com/
Do let me know if you like this blog. Thank you..!!!

Comments

Popular posts from this blog

Sonarqube Integration with Visual Studio 2022 and VS Code

Run And Debug Test Cases Using Jest With Different CLI Options

Install Kibana as Windows Service Using Powershell