Skip to content

git

Free and open source distributed version control system.

  • Not a file delta‑based (difference) VCS
  • Store a series of references of snapshots i.e stream of snapshots
  • Integrity is verified by checksums i.e SHA‑1 used for checksum
  • Stages
    • Modified = Working Directory
    • Staged = Staging Area
    • Committed = Repository
  • Offline documentation can be accessed using man git or git help <COMMAND> or git <COMMAND> -h

git config maintains the configuration settings for all the repositories and users.

ScopeCommand to View (git config)LocationRemark
System--system/etc/gitconfigAll users and repos
Global--global~/.gitconfig or ~/.config/git/configOne user and all repos
Local--local.git/configOne user and one repo
Terminal window
# List the current used configs
git config --list --show-origin
# Set the user related configs
git config --global user.name "<YOUR_NAME>"
git config --global user.email "<YOUR_EMAIL>"
# Default Editor used by git
git config --global core.editor vim
# Specify default branch for the repos
git config --global init.defaultBranch main
# Getting single config
git config <CONFIG_KEY>
git config user.name
  • Types
    • Tracked files (Committed + Staged files)
    • Untracked files
  • Lifecycle
    • Untracked
    • Unmodified
    • Modified
    • Staged
  • .gitignore file is used to specify untracked files
    • lines starting with # are comments and are ignored
    • lines starting with ! are skipped and not included in the untracked files
Terminal window
git init # Initialize an empty git repository
# To clone a existing repository
git clone <URL>
git clone <URL> <CUSTOM_DIRECTORY_NAME> # Clone the repo in a user–defined directory
# Add files for tracking
git add <FILE_NAME/DIRECTORY_NAME> # Recursively add the files
# Checking the status of the Tracked / Untracked files
git status
git status --short or git status -s # Short status
# Committing the changes
git commit -m "COMMIT_MESSAGE"
git commit -a -m "COMMIT_MESSAGE" # Staging and committing the changes in a single command
Terminal window
# Checking the changes
git diff # What changes but NOT staged
git diff --staged # what changed between staged and the last committed files
git difftool # GUI diff using the default GUI diff tool
Terminal window
# Checking the history and previous commits in reverse chronological order
git log
git log -p # Display history along with the diff changes for each commit
git log --stat # Display history along with the changes count for each file
git log --pretty=oneline # Display only commit messages
git log --oneline # Display only commit messages
git log --graph # Clean graph view from all the branches
git log -<n> # Top n commits to view
git log --since <CUSTOM_DATE_TIME> # Only shows commits since the given datetime
git log --until <CUSTOM_DATE_TIME> # Only shows commits until the given datetime
git log -S STRING_TO_TRACK # Returns the commits in which the given STRING_TO_TRACK was changed/Updated/Added/Deleted

Making additional changes to the last committed commit

Terminal window
git commit --amend # Keeps the same commit message but adds the new staged files to the commit
git cherry‑pick <COMMIT_ID> # Pick a specific commit and add it to current checked out branch
Terminal window
git remote # Lists all the remotes
git remote -v # Lists the remotes along with the urls
git remote add <REMOTE_NAME> <REMOTE_URL> # Add a separate remote
git remote add upstream <REMOTE_UPSTREAM_URL> # Add an upstream
git remote show <REMOTE> # Show details of the changes between the remote and local
git remote remove <REMOTE> # Remove a remote from local
git remote rename <OLD_REMOTE_NAME> <NEW_REMOTE_NAME>
git fetch <REMOTE> # Pulls the changes to the local from remote and DON'T Merge. Manual Merging required
git pull <REMOTE> # Pulls the changes and merges to local as well
git pull <REMOTE_NAME> <BRANCH_NAME> # Pulls a specific remote branch
git config --global pull.rebase "true" # Rebases when merging
  • Types
    • Lightweight ‑ Pointer to specific commit
    • Annotated ‑ Explicitly stored in the commit object along with additional info like tag user name etc
Terminal window
git tags -l / --list # List the tags existing
git show <TAG_NAME> # Display a tag
git tag <TAG_NAME> # Create a lightweight tag
git tag <TAG_NAME> <COMMIT_HASH> # Create a lightweight tag for an old commit
git tag -a v0.1.1 -m "<TAG_NAME>" # Create an annotated tag
git push origin <TAG_NAME> # By default, tags are not pushed to remote. Manually push
git tag -d <TAG_NAME> # Delete a tag locally
git push origin --delete <TAG_NAME> # Delete a tag from remote
git checkout -b <NEW_BRANCH_NAME> <TAG_NAME> # Creating a new Branch from a tag
Terminal window
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
  • Stores a series of snapshots
  • commits store pointers to commit objects
Terminal window
git branch <NEW_BRANCH> # Only Creates a new branch, Doesn’t switch to that branch
git branch -d <BRANCH> # Delete a branch
git checkout -b <NEW_BRANCH> # Creates a new branch and switch to that branch
git checkout <BRANCH> # Switches the HEAD pointer to that branch
git switch <BRANCH> # Checkout this branch
git switch -c <NEW_BRANCH> # Creates a new branch and switches to that branch.
  • merges diverged code commits
Terminal window
git checkout <MASTER_BRANCH> # Checkout to the branch to which you need to merge the branch changes
git merge <BRANCH_NAME> # merges the BRANCH_NAME changes to the current checked out branch i.e MASTER_BRANCH
  • Save local changes without committing
Terminal window
git stash # saves local changes to stash
git stash list # show the stashed changes
git stash pop # load the last stashed changes into the working directory
git stash pop stash@{0} # Pop a specific stash number instead of the latest one
git stash drop stash@{0} # Drop a specific stashed changes