Skip to content
Anuj Jain edited this page Jan 12, 2021 · 4 revisions

Git Basics

Basic Git workflow

  1. You modify files in your working directory.
  2. You stage changes you want to commit.
  3. You commit to create a snapshot and store in repository.

Git have 3 different state for your files

Working Directory Staging Area .git Repository
Create / Modify files Staging for next commit metadata and object database
state: modified state: staged state: committed

First time Git setup

Git config settings are at 3 different levels:

  • local
  • global
  • system.

You can view all of git settings and source level:

$ git config --list --show-origin

Your identity

git config --global user.name "John Doe"

git config --global user.email johndoe@example.com

Getting help

$ git help <verb>
$ git <verb> --help
$ man git-<verb>

Cloning an existing repository

$ git clone https://github.com/JavaMasterClass/JavaMasterClass.github.io.git

Checking status of your files

$ git status

Track new files and stage modified files

$ git add FILENAME

Ignoring Files

Add file patten to .gitignore

Committing Your Changes

git commit -m "Commit Message"

Removing Files

$ git rm FILENAME

Commit history

$ git log


Undo things

Undo last commit (before pushing it to remote repo)

$ git commit --amend

Unstaging a Staged File

$ git reset HEAD FILENAME
$ git restore --staged FILENAME

Un-modifying a Modified File (last committed state)

$ git checkout -- FILENAME
alternative command
$ git restore FILENAME

Working with your remote repository

Showing your remote repository urls

git remote -v

Fetching and Pulling from Your Remotes

# Get data from your remote projects
$ git fetch <remote>

# fetch data and automatically tries to merge
$ git pull

Pushing to Your Remotes

# git push <remote> <branch>
$ git push origin master

Inspecting a Remote

$ git remote show origin