git
Free and open source distributed version control system.
Main Features
Section titled “Main Features”- 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 gitorgit help <COMMAND>orgit <COMMAND> -h
First Time Setup / Config
Section titled “First Time Setup / Config”git config maintains the configuration settings for all the repositories and users.
| Scope | Command to View (git config) | Location | Remark |
|---|---|---|---|
| System | --system | /etc/gitconfig | All users and repos |
| Global | --global | ~/.gitconfig or ~/.config/git/config | One user and all repos |
| Local | --local | .git/config | One user and one repo |
# List the current used configsgit config --list --show-origin
# Set the user related configsgit config --global user.name "<YOUR_NAME>"git config --global user.email "<YOUR_EMAIL>"
# Default Editor used by gitgit config --global core.editor vim
# Specify default branch for the reposgit config --global init.defaultBranch main
# Getting single configgit config <CONFIG_KEY>git config user.nameBasics
Section titled “Basics”- Types
- Tracked files (Committed + Staged files)
- Untracked files
- Lifecycle
- Untracked
- Unmodified
- Modified
- Staged
.gitignorefile 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
- lines starting with
Add, clone, status, commit commands
Section titled “Add, clone, status, commit commands”git init # Initialize an empty git repository
# To clone a existing repositorygit clone <URL>git clone <URL> <CUSTOM_DIRECTORY_NAME> # Clone the repo in a user–defined directory
# Add files for trackinggit add <FILE_NAME/DIRECTORY_NAME> # Recursively add the files
# Checking the status of the Tracked / Untracked filesgit statusgit status --short or git status -s # Short status
# Committing the changesgit commit -m "COMMIT_MESSAGE"git commit -a -m "COMMIT_MESSAGE" # Staging and committing the changes in a single command# Checking the changesgit diff # What changes but NOT stagedgit diff --staged # what changed between staged and the last committed filesgit difftool # GUI diff using the default GUI diff tool# Checking the history and previous commits in reverse chronological ordergit loggit log -p # Display history along with the diff changes for each commitgit log --stat # Display history along with the changes count for each filegit log --pretty=oneline # Display only commit messagesgit log --oneline # Display only commit messagesgit log --graph # Clean graph view from all the branchesgit log -<n> # Top n commits to viewgit log --since <CUSTOM_DATE_TIME> # Only shows commits since the given datetimegit log --until <CUSTOM_DATE_TIME> # Only shows commits until the given datetimegit log -S STRING_TO_TRACK # Returns the commits in which the given STRING_TO_TRACK was changed/Updated/Added/DeletedBasics ‑ II
Section titled “Basics ‑ II”Making additional changes to the last committed commit
git commit --amend # Keeps the same commit message but adds the new staged files to the commitgit cherry‑pick <COMMIT_ID> # Pick a specific commit and add it to current checked out branchRemote Repository Management
Section titled “Remote Repository Management”git remote # Lists all the remotesgit remote -v # Lists the remotes along with the urlsgit remote add <REMOTE_NAME> <REMOTE_URL> # Add a separate remotegit remote add upstream <REMOTE_UPSTREAM_URL> # Add an upstreamgit remote show <REMOTE> # Show details of the changes between the remote and localgit remote remove <REMOTE> # Remove a remote from localgit 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 requiredgit pull <REMOTE> # Pulls the changes and merges to local as wellgit pull <REMOTE_NAME> <BRANCH_NAME> # Pulls a specific remote branch
git config --global pull.rebase "true" # Rebases when mergingTagging
Section titled “Tagging”- Types
- Lightweight ‑ Pointer to specific commit
- Annotated ‑ Explicitly stored in the commit object along with additional info like tag user name etc
git tags -l / --list # List the tags existinggit show <TAG_NAME> # Display a taggit tag <TAG_NAME> # Create a lightweight taggit tag <TAG_NAME> <COMMIT_HASH> # Create a lightweight tag for an old commitgit 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 locallygit push origin --delete <TAG_NAME> # Delete a tag from remote
git checkout -b <NEW_BRANCH_NAME> <TAG_NAME> # Creating a new Branch from a taggit config --global alias.co checkoutgit config --global alias.br branchgit config --global alias.ci commitgit config --global alias.st statusBranching
Section titled “Branching”- Stores a series of snapshots
- commits store pointers to commit objects
git branch <NEW_BRANCH> # Only Creates a new branch, Doesn’t switch to that branchgit branch -d <BRANCH> # Delete a branch
git checkout -b <NEW_BRANCH> # Creates a new branch and switch to that branchgit checkout <BRANCH> # Switches the HEAD pointer to that branch
git switch <BRANCH> # Checkout this branchgit switch -c <NEW_BRANCH> # Creates a new branch and switches to that branch.Merging
Section titled “Merging”- merges diverged code commits
git checkout <MASTER_BRANCH> # Checkout to the branch to which you need to merge the branch changesgit merge <BRANCH_NAME> # merges the BRANCH_NAME changes to the current checked out branch i.e MASTER_BRANCHStashing
Section titled “Stashing”- Save local changes without committing
git stash # saves local changes to stashgit stash list # show the stashed changesgit stash pop # load the last stashed changes into the working directorygit stash pop stash@{0} # Pop a specific stash number instead of the latest onegit stash drop stash@{0} # Drop a specific stashed changes