Distributed Version Control

GIT

https://git-scm.com

GIT

Basics

Advantages of GIT

  • No need to communicate with central server
  • Faster
  • No network access required
  • No single failure point
  • Encourages participation and forking
  • Devs can work independently

Basics about GIT

  • Head is the time machine
  • Head points to the latest working branch
  • Point the head to any branch
  • Staged is an alias for cached
  • GIT Three tree: Working directory - Staging area - Repository
  • git doesn't track empty folders
  • Exclude any file or folder by adding to
  • File name = ".gitignore"

New

GIT Project

Git init
Git add README.md
Git commit -m "first commit"
Git branch -M main
Git remote add origin https://github.com/cccc
Git push -u origin main

Setup your new GIT project like the above.

GIT

Most used commands

git init

Creates a new Git repository in the current directory.

git status

Shows staged, unstaged, and untracked changes.

git log --oneline -n 10

Shows the 10 most recent commits in a compact format.

git branch

Lists local branches and marks the current branch.

git switch branchName

Switches to an existing branch named branchName.

git switch -c branchName

Creates branchName from HEAD and switches to it.

git checkout branchName

Switches to an existing branch using the older command.

git checkout -b branchName

Creates a branch from HEAD and switches to it using the older command.

git branch -d branchName

Deletes a fully merged local branch.

git add .

Stages changes in the current directory and its subdirectories.

git commit -m "your commit message here"

Commits staged changes with the supplied message.

GIT

Configuration

git config --list

Lists all Git configuration values currently in effect.

git config user.name

Shows the user name configured for the current repository.

git config user.email

Shows the email address configured for the current repository.

git config --global user.name "Gautam"

Sets Gautam as the default author name for all repositories.

git config --global user.email "gautam@iwiz.in"

Sets the default author email address for all repositories.

git config --global core.editor "vscode.exe"

Sets Visual Studio Code as Git's default text editor.

git config --global core.editor "code --wait"

Sets Visual Studio Code as Git's default editor and makes Git wait until the editor tab is closed.

GIT

Staging

git add .

Stages all new and modified files in the current directory.

git add "file name"

Stages only the named file.

git add -A

Stages all changes in the repository, including new, modified, and deleted files.

git add --all

Long-form equivalent of git add -A.

Inspect

Repository status

git status

Shows changes that are staged, changes that are not staged, and untracked files.

git status --short

Shows a compact status. ?? means untracked and M means modified; the two columns distinguish staged changes from unstaged changes.

Working tree

Restoring changes

git restore .

Discards unstaged changes to tracked files in the current directory. It does not affect staged changes or untracked files.

git restore "file name"

Discards unstaged changes to one tracked file. It does not affect a staged version of the file or any untracked file.

git checkout -- .

Legacy equivalent for discarding unstaged changes to tracked files in the current directory.

git restore --staged "file name"

Unstages one file while keeping its working-directory changes.

GIT

Committing

git commit -m "commit message"

Records a snapshot of the staged changes with a commit message.

git commit -am "message"

Stages modified and deleted tracked files, then commits them. New, untracked files are not included.

git commit -a

Stages modified and deleted tracked files, then opens the editor for a multiline commit message. New, untracked files are not included.

Index and working tree

Reset

git reset

Unstages all staged changes while preserving the files in the working directory.

git reset .

Unstages changes under the current directory while preserving the working files.

git reset --hard

Resets tracked files and the index to the current commit. This permanently discards staged and unstaged changes to tracked files.

Configuration

Ignoring files

git config --global core.excludesFile "path/to/global.gitignore"

Sets an ignore file that applies to every local repository.

git rm -r --cached .

Removes tracked files from the index without deleting local copies. Add the files again to apply current .gitignore rules.

GIT

Add a new change to the previous commit

git commit --amend

Adds currently staged changes to the previous commit and opens the editor so you can update its commit message.

git commit --amend --no-edit

Adds currently staged changes to the previous commit while keeping its existing message.

git commit --amend -m "new commit message"

Amends only the previous commit message when nothing new is staged.

Amending replaces the previous commit with a new commit containing its original changes plus any newly staged changes. This creates a new commit hash, so avoid amending commits already shared with others.

git checkout

Switches branches or restores files in the working directory. For newer workflows, Git also provides git switch and git restore.

Working tree

Stashing code

git stash

Temporarily stores staged and unstaged changes to tracked files and restores a clean working tree.

git stash save "message"

Saves a named stash using the older syntax. Prefer git stash push -m "message" in new workflows.

git stash list

Lists all stashes and their identifiers.

git stash apply stash@{0}

Applies the stash at index 0 without removing it from the stash list.

git stash pop

Applies the latest stash, at index 0, then removes it.

Editor

Commit message editor

Vim: Esc, then :wq, then Enter

Saves the commit message, closes Vim, and completes the commit.

Vim: Esc, then ZZ

Another way to save the commit message and close Vim.

Vim: Esc, then :q!, then Enter

Closes Vim without saving changes to the commit message.

Vim: Esc, then :cq, then Enter

Closes Vim with an error so Git cancels the commit operation.

Nano: Ctrl+O, Enter, then Ctrl+X

Saves the commit message and exits Nano.

Nano: Ctrl+X, then N

Exits Nano without saving the edited commit message.

VS Code: Ctrl+S, then close the commit message tab

Saves the message and allows Git to complete the commit.

GIT

Log of all commits

git log

Shows the repository's commit history with full details.

git log --oneline

Shows each commit in a compact, single-line format.

git log -n 5

Shows only the five most recent commits.

git log --since=2019-01-01

Shows commits made on or after the specified date.

git log --until=2019-01-01

Shows commits made on or before the specified date.

git log --author="kevin"

Shows commits whose author matches the supplied name.

git log --grep="bug fixes"

Shows commits whose messages match the supplied regular expression.

Log pager: f = forward, b = backward, q = quit

Navigates the pager used for long Git output, including git log.

GIT

Branches

git branch

Lists local branches and marks the current branch.

git branch -a

Lists local and remote-tracking branches.

git branch BRANCH_NAME

Creates a branch without switching to it.

git switch -c BRANCH_NAME

Creates a branch and switches to it.

git checkout -b BRANCH_NAME

Legacy command that creates a branch and switches to it.

git switch BRANCH_NAME

Switches to an existing branch.

git checkout BRANCH_NAME

Switches to an existing branch. Unlike git switch, git checkout can also restore files.

git merge "branch name"

Merges the named branch into the current branch. For example, to merge somebranch into main, switch to main first, then run git merge somebranch.

git branch --delete BRANCH_NAME

Deletes a fully merged local branch.

git branch -d BRANCH_NAME

Short form of git branch --delete.

git branch -D BRANCH_NAME

Force-deletes a local branch, even when it is not fully merged.

Branches

Rename master to main

git branch -m master main

Renames the local master branch to main.

git branch -M main

Renames the current branch to main, forcing the rename if necessary.

git push -u origin main

Pushes main and sets origin/main as its upstream branch.

git push origin --delete master

Deletes the old remote master branch after the remote's default branch has been changed to main.

History

Rebasing

git rebase

Rebases the current branch onto its configured upstream branch.

git rebase BRANCH_OR_COMMIT

Replays the current branch's commits onto a branch or commit.

git rebase -i HEAD~NUMBER

Interactively edits, combines, reorders, or removes the specified number of recent commits.

git rebase -i --root

Starts an interactive rebase that includes the first commit.

GIT

Remotes

git remote add NAME URL

Adds a remote repository with the specified name and URL.

git remote remove NAME

Removes a remote from the local repository configuration.

git remote rename OLD_NAME NEW_NAME

Renames an existing remote.

git remote -v

Lists remotes and their fetch and push URLs.

git remote set-url origin https://url

Changes the URL assigned to the origin remote.

Remote

Pushing

git push REMOTE BRANCH

Pushes the specified local branch to a remote.

git push --set-upstream origin main

Pushes main and sets its upstream tracking branch.

git push -u origin main

Short form of the preceding set-upstream command.

git push --all origin

Pushes all local branches to origin.

git branch --set-upstream-to=origin/main main

Sets the local main branch to track origin/main without pushing.

Resources

Misc

https://github.com/ohmyzsh/ohmyzsh

Oh My Zsh is an open-source framework for managing Zsh configuration, with plugins and themes that improve Git workflows in the command line.