Repository Initialization

Initialize a new Git repository.

$ git init

Clone a remote repository to the local machine.

$ git clone repository_url

Basic Commands

Stage changes for the next commit.

$ git add file_name

Commit staged changes with a message.

$ git commit -m 'Commit message'

Show the status of changes as untracked, modified, or staged.

$ git status

Display commit history.

$ git log

Show changes between commits, the working directory, and the staging area.

$ git diff

Branching and Merging

Create a new branch.

$ git branch branch_name

Switch to a different branch.

$ git checkout branch_name

Merge changes from a branch into the current branch.

$ git merge branch_name

Delete a branch.

$ git branch -d branch_name

Remote Repositories

Show remote repositories.

$ git remote -v

Fetch changes from a remote repository and merge them into the current branch.

$ git pull origin branch_name

Push changes from a local branch to a remote repository.

$ git push origin branch_name

Tagging

Create an annotated tag.

$ git tag -a tag_name -m 'Tag message'

Show information about a tag.

$ git show tag_name

Push tags to a remote repository.

$ git push origin tag_name

Undoing Changes

Discard all changes in the working directory and staging area.

$ git reset --hard HEAD

Create a new commit that undoes changes made in a previous commit.

$ git revert commit_hash

Stash changes in the working directory for later use.

$ git stash

Advanced Git Operations

Rebasing

Reapply commits on top of another branch.

$ git rebase branch_name

Interactive rebase for the last 3 commits.

$ git rebase -i HEAD~3

Abort an ongoing rebase.

$ git rebase --abort

Cherry-Picking

Apply a specific commit to the current branch.

$ git cherry-pick commit_hash

Apply changes from a commit without committing immediately.

$ git cherry-pick -n commit_hash

Submodules

Add a submodule to the repository.

$ git submodule add submodule_url

Initialize and update submodules.

$ git submodule update --init --recursive

Update all submodules to the latest commit on their master branch.

$ git submodule foreach 'git checkout master && git pull'

Git Workflows

Start a new feature branch using Git Flow.

$ git flow feature start feature_name

Start a new release using Git Flow.

$ git flow release start version

Start a new hotfix using Git Flow.

$ git flow hotfix start version

Configuration

Set the global username for Git.

$ git config --global user.name 'Your Name'

Set the global email for Git.

$ git config --global user.email 'your.email@example.com'

Set the default text editor for Git.

$ git config --global core.editor 'editor_name'

Collaboration and Remote Repositories

Collaboration

Add an upstream repository for collaborative work.

$ git remote add upstream repository_url

Fetch changes from the upstream repository.

$ git fetch upstream

Merge changes from the upstream repository into your local branch.

$ git merge upstream/main

Push your changes to your fork on GitHub.

$ git push origin your_branch

Git Hooks

Make a pre-commit hook executable.

$ chmod +x .git/hooks/pre-commit

Edit the pre-commit hook script.

$ vi .git/hooks/pre-commit

Start a bisect session to find the commit that introduced a bug.

$ git bisect start

Mark the current commit as good in a bisect session.

$ git bisect good

Mark the current commit as bad in a bisect session.

$ git bisect bad

Git Archive

Create a zip archive of the master branch.

$ git archive --format=zip --output=archive.zip master

Create a tar.gz archive of a specific release.

$ git archive --format=tar.gz --output=archive.tar.gz release-1.0

Git Subtree

Add a remote repository as a subtree in a subfolder.

$ git subtree add --prefix=subfolder repository_url master --squash

Pull changes from a subtree repository.

$ git subtree pull --prefix=subfolder repository_url master --squash

Git History and Information

Git Log Customization

Show each commit on a single line.

$ git log --oneline

Display a text-based graph of commits.

$ git log --graph

Show commits by a specific author.

$ git log --author='John Doe'

Show commits since a specific date.

$ git log --since='3 days ago'

Search for commits with a specific keyword.

$ git log --grep='keyword'

Git Reflog

Show a log of all changes to HEAD.

$ git reflog

Show the reflog for a specific branch.

$ git reflog show branch_name

Remove old entries from the reflog.

$ git reflog expire --expire=now --all

Git Bisect

Start a bisect session to find the commit that introduced a bug.

$ git bisect start

Mark the current commit as good in a bisect session.

$ git bisect good

Mark the current commit as bad in a bisect session.

$ git bisect bad

Complete the bisect session and return to the original branch.

$ git bisect reset

Git Worktree

Creating Worktrees

Create a new worktree with a new branch.

$ git worktree add -b new_branch path

Create a new worktree from an existing branch.

$ git worktree add path existing_branch

List all linked worktrees.

$ git worktree list

Remove a linked worktree.

$ git worktree remove path

Git Rerere

Enable the Rerere (Reuse Recorded Resolution) feature globally.

$ git config --global rerere.enabled true

Manually run the Rerere conflict resolution.

$ git rerere

Clear the Rerere conflict resolutions.

$ git rerere clear

Git LFS (Large File Storage)

Install Git LFS globally.

$ git lfs install

Track large files with Git LFS.

$ git lfs track '*.zip'

Push all Git LFS objects to the remote repository.

$ git lfs push --all origin

Git Subversion (git-svn)

Clone a Subversion repository using git-svn.

$ git svn clone svn_repository_url

Fetch new revisions from the Subversion repository.

$ git svn fetch

Rebase your changes on top of the latest Subversion revisions.

$ git svn rebase

Git Stash

Stash changes in the working directory.

$ git stash

List all stashes.

$ git stash list

Apply the latest stash.

$ git stash apply

Remove the latest stash.

$ git stash drop