Skip to content
Soumya Ranjan Choudhury edited this page Aug 22, 2023 · 5 revisions

Git is a version control system used for tracking changes in computer files. It is generally used for source code management in software development.

  • The distributed version control tool is used for source code management
  • It allows multiple developers to work together
  • It supports non-linear development through its thousands of parallel branches

Features

  • Tracks history
  • Free and open source
  • Supports non-linear development
  • Creates backups
  • Scalable
  • Supports collaboration
  • Branching is easier
  • Distributed development

Workflow

The Git workflow is divided into three states:

  • Working directory - Modify files in your working directory
  • Staging area (Index) - Stage the files and add snapshots of them to your staging area
  • Git directory (Repository) - Perform a commit that stores the snapshots permanently to your Git directory. Checkout any existing version, make changes, stage them and commit.

Branch

Branch in Git is used to keep your changes until they are ready. You can do your work on a branch while the main branch (master) remains stable. After you are done with your work, you can merge it with the main office.

Commands

Setup

Command Description
git config --global user.name "[username]" set a name that is identifiable for credit when review version history
git config --global user.email "[email-id]" to add email into your local config
git config --global user.password "[password]" to add password into your local config
git config --list --show-origin To check the git config
git config --global color.ui auto set automatic command line coloring for Git for easy reviewing

Getting & Creating Projects

Command Description
git init Initialize a local Git repository
git clone ssh://git@github.com/[username]/[repository-name].git Create a local copy of a remote repository

Basic Snapshotting

Command Description
git status Check status
git add [file-name.txt] Add a file to the staging area
git add -A Add all new and changed files to the staging area
git reset [file-name] unstage a file while retaining the changes in working directory
git commit -m "[commit message]" commit your staged content as a new commit snapshot
git rm -r [file-name.txt] Remove a file (or folder)

Branching & Merging

Command Description
git branch List branches (the asterisk denotes the current branch)
git branch -a List all branches (local and remote)
git branch [branch name] Create a new branch
git branch -d [branch name] Delete a branch
git push origin --delete [branch name] Delete a remote branch
git checkout -b [branch name] Create a new branch and switch to it
git checkout -b [branch name] origin/[branch name] Clone a remote branch and switch to it
git branch -m [old branch name] [new branch name] Rename a local branch
git checkout [branch name] Switch to a branch
git checkout - Switch to the branch last checked out
git checkout -- [file-name.txt] Discard changes to a file
git merge [branch name] Merge a branch into the active branch
git merge [source branch] [target branch] Merge a branch into a target branch

Sharing & Updating Projects

Command Description
git push origin [branch name] Push a branch to your remote repository
git push -u origin [branch name] Push changes to remote repository (and remember the branch)
git push Push changes to remote repository (remembered branch)
git push origin --delete [branch name] Delete a remote branch
git pull Update local repository to the newest commit
git pull origin [branch name] Pull changes from remote repository
git remote add origin ssh://git@github.com/[username]/[repository-name].git Add a remote repository
git remote set-url origin ssh://git@github.com/[username]/[repository-name].git Set a repository's origin branch to SSH

Inspection & Comparison

Command Description
git log View changes
git log --summary View changes (detailed)
git log --oneline View changes (briefly)
git log [source branch] [target branch] show the commits on target branch that are not on source branch
git log --follow [file-name] show the commits that changed file, even across renames
git diff diff of what is changed but not staged
git diff --staged diff of what is staged but not yet commited
git diff [source branch] [target branch] Preview changes before merging

Temporary commits

Command Description
git stash Save modified and staged changes
git stash list list stack-order of stashed file changes
git stash pop write working from top of stash stack
git stash drop discard the changes from top of stash stack
git stash clear Remove all stashed entries

Rewrite History

Command Description
git rebase [branch] apply any commits of current branch ahead of specified one
git reset --hard [commit] clear staging area, rewrite working tree from specified commit

Clone this wiki locally