Comprehensive Guide to Essential Git Commands

Comprehensive Guide to Essential Git Commands

Git is a powerful version control system widely used by developers to manage code and collaborate on projects. This guide provides a cheat sheet of essential Git commands, categorized for easy reference. Whether you're new to Git or an experienced user, these commands will help streamline your workflow.

Basic Commands

Master Branch

  • master: The default development branch.

HEAD

  • HEAD: Represents the current commit.
  • HEAD^: Parent of HEAD.
  • HEAD~4: Fourth ancestor of HEAD.

Branch Navigation

  • foo...bar: From branch foo to branch bar.

Help

  • git help [command]: Displays help information for a specific command.

Creating Repositories

From Existing Files

  • git init: Initializes a new Git repository.

From Existing Repository

  • git clone [URL]: Clones an existing repository from a specified URL.

Publishing Changes

Committing Changes

  • git commit -a: Commits all changed files.
  • git commit -m "[msg]": Commits with a message.
  • git commit -am "[msg]": Adds and commits with a message.

Tagging and Remote

  • git tag [name]: Tags the current version.
  • git remote add origin [URL]: Adds a remote repository.
  • git push: Pushes changes to the remote repository.

Updating Repositories

Fetching and Pulling

  • git fetch: Fetches from the default upstream.
  • git pull: Fetches and merges from the remote repository.

Patching

  • git am [patch.mbox]: Applies a patch from a mailbox.
  • git apply [patch.diff]: Applies a patch directly.

Viewing Changes

  • git status: Shows the status of changes.
  • git diff [old] [new]: Shows differences between commits.
  • git log [-p] [file | dir]: Shows the commit log.
  • git blame [file]: Shows who changed each line.
  • git show [id]: Displays commit metadata.
  • git shortlog: Provides a summary of commits.
  • git tag -l: Lists tags.

Reverting Changes

  • git reset --hard: Resets without undo.
  • git reset: Resets to the last commit.
  • git revert [commit]: Reverts a specific commit.
  • git commit --amend: Amends the last commit.
  • git checkout [id] [file]: Checks out a file from a specific commit.

Branch Management

  • git checkout [branch]: Switches branches.
  • git merge [branch]: Merges specified branch into the current branch.
  • git branch [branch]: Creates a new branch.
  • git branch -d [branch]: Deletes a branch.

Handling Conflicts

  • git diff [--base]: Shows base diff.
  • git diff --ours: Shows diff from our side.
  • git diff --theirs: Shows diff from their side.
  • git log --merge: Logs merges.
  • gitk --merge: Visual merge tool.

Useful Tools

  • git archive: Creates a release tarball.
  • git bisect: Binary search for defects.
  • git cherry-pick: Takes a single commit.
  • git fsck: Checks repository integrity.
  • git gc: Compresses metadata.
  • git rebase: Forwards local changes.
  • git remote add [URL]: Adds a remote URL.
  • git stash: Temporarily sets aside changes.

Tracking Files

  • git add [files]: Adds files to staging.
  • git rm [files]: Removes files.
  • git mv [old] [new]: Renames files.
  • git ls-files: Lists files in the working directory.

Local Repository Structure

  • Working Directory: The directory where you work on files.
  • Local Repository: Contains the current branch and other branches.
  • Remote Repository: The origin repository for push and pull operations.

Command Flow

  1. Create: git init or git clone.
  2. Browse: git status, git log, git diff.
  3. Change: git add, git commit.
  4. Revert: git reset, git checkout.
  5. Update: git fetch, git pull.
  6. Branch: git branch, git checkout.
  7. Commit: git commit.
  8. Push: git push.

This guide should serve as a quick reference for essential Git commands. Mastering these commands will enhance your efficiency and collaboration in any Git-based project.

要查看或添加评论,请登录

Shishir Srivastav的更多文章

社区洞察

其他会员也浏览了