Bigdata – Knowledge Base

Git – All Commands

We are creating a comprehensive list of Git commands is a significant undertaking, given the vast number of commands and options available. Below is a detailed list of commonly used Git commands, categorized for easier navigation. This list includes basic commands, advanced commands, configuration, collaboration, branching, and more.


1. Basic Git Commands #

1.1 Initialization & Cloning #

  • git init
    Initializes a new Git repository.
  • git clone <repository>
    Clones a repository into a new directory.
  • git clone <repository> <directory>
    Clones a repository into a specific directory.

1.2 Staging & Committing #

  • git add <file>
    Adds a file to the staging area.
  • git add .
    Stages all changes in the current directory.
  • git commit -m "message"
    Commits the staged changes with a message.
  • git commit -a -m "message"
    Stages and commits all changes.

1.3 Viewing the Status #

  • git status
    Shows the working directory status.
  • git diff
    Displays changes not staged for commit.
  • git diff --staged
    Displays changes staged for the next commit.

1.4 Viewing the History #

  • git log
    Displays the commit history.
  • git log --oneline
    Shows a concise commit history.
  • git log --graph
    Displays the commit history as a graph.
  • git log -p
    Shows the patch (diff) introduced in each commit.
  • git log --stat
    Displays the file-wise changes in each commit.

1.5 Removing & Renaming Files #

  • git rm <file>
    Removes a file from the working directory and staging area.
  • git mv <oldname> <newname>
    Renames a file and stages the change.

1.6 Ignoring Files #

  • .gitignore
    Specifies files and directories to be ignored by Git.

2. Branching & Merging #

2.1 Branching #

  • git branch
    Lists all branches.
  • git branch <branch>
    Creates a new branch.
  • git branch -d <branch>
    Deletes a branch (if fully merged).
  • git branch -D <branch>
    Forcefully deletes a branch.

2.2 Switching Branches #

  • git checkout <branch>
    Switches to the specified branch.
  • git checkout -b <branch>
    Creates and switches to a new branch.

2.3 Merging #

  • git merge <branch>
    Merges the specified branch into the current branch.
  • git merge --no-ff <branch>
    Merges the branch with a merge commit, even if a fast-forward is possible.

2.4 Rebasing #

  • git rebase <branch>
    Reapplies commits on top of another base tip.
  • git rebase --interactive <commit>
    Rebases with an interactive mode, allowing commit reordering, squashing, etc.
  • git rebase --continue
    Continues rebasing after resolving conflicts.

2.5 Stashing #

  • git stash
    Temporarily saves changes not ready to commit.
  • git stash apply
    Applies the stashed changes.
  • git stash pop
    Applies and removes the latest stash.
  • git stash list
    Lists all stashes.

3. Remote Repositories #

3.1 Working with Remotes #

  • git remote
    Lists the remote repositories.
  • git remote add <name> <url>
    Adds a new remote repository.
  • git remote remove <name>
    Removes a remote repository.
  • git remote -v
    Shows detailed information about remotes.

3.2 Fetching & Pulling #

  • git fetch <remote>
    Fetches changes from a remote repository.
  • git pull
    Fetches and merges changes from a remote repository.
  • git pull --rebase
    Fetches changes and rebases the local commits.

3.3 Pushing #

  • git push <remote> <branch>
    Pushes local changes to a remote branch.
  • git push -u <remote> <branch>
    Sets the upstream branch and pushes changes.
  • git push --force
    Forcefully pushes changes, potentially overwriting history.

3.4 Tagging #

  • git tag <tagname>
    Creates a tag at the current commit.
  • git tag -a <tagname> -m "message"
    Creates an annotated tag with a message.
  • git push <remote> <tagname>
    Pushes a specific tag to a remote repository.
  • git push --tags
    Pushes all tags to a remote repository.

4. Undoing Changes #

4.1 Reverting & Resetting #

  • git revert <commit>
    Creates a new commit that undoes changes from a specified commit.
  • git reset <file>
    Unstages a file without altering its content.
  • git reset --hard <commit>
    Resets the index and working directory to a specific commit.
  • git reset --soft <commit>
    Resets the index to a specific commit but leaves working directory intact.
  • git reset --mixed <commit>
    Resets the index to a specific commit but keeps changes in the working directory.

4.2 Cleaning #

  • git clean -f
    Removes untracked files from the working directory.
  • git clean -fd
    Removes untracked files and directories.

4.3 Amending Commits #

  • git commit --amend
    Modifies the most recent commit.
  • git commit --amend -m "message"
    Changes the commit message of the most recent commit.

5. Viewing & Comparing #

5.1 Comparing Commits #

  • git diff <commit1> <commit2>
    Shows differences between two commits.
  • git diff <branch1> <branch2>
    Shows differences between two branches.

5.2 Comparing with HEAD #

  • git diff HEAD
    Shows differences between the working directory and the latest commit.
  • git diff --cached
    Shows differences between the staging area and the latest commit.

5.3 Blaming & Annotating #

  • git blame <file>
    Displays the last modification for each line in a file.
  • git annotate <file>
    Shows who last modified each line of a file (synonym for git blame).

6. Advanced Commands #

6.1 Bisecting #

  • git bisect start
    Starts the binary search to find the commit that introduced a bug.
  • git bisect good <commit>
    Marks a commit as good.
  • git bisect bad <commit>
    Marks a commit as bad.
  • git bisect reset
    Ends the bisect session and returns to the original branch.

6.2 Cherry-Picking #

  • git cherry-pick <commit>
    Applies changes from a specific commit.

6.3 Submodules #

  • git submodule add <repository>
    Adds a new submodule to the repository.
  • git submodule init
    Initializes the submodule configuration.
  • git submodule update
    Clones or updates the submodules.
  • git submodule deinit <path>
    Deinitializes a submodule.

6.4 Working with Patches #

  • git format-patch <commit>
    Creates a patch file for a specific commit.
  • git apply <patchfile>
    Applies a patch file to the working directory.

7. Configuration & Customization #

7.1 User Configuration #

  • git config --global user.name "name"
    Sets the global username.
  • git config --global user.email "email"
    Sets the global email address.
  • git config --global core.editor <editor>
    Sets the default text editor for Git.
  • git config --global color.ui auto
    Enables colored output in Git commands.

7.2 Aliases #

  • git config --global alias.<alias> <command>
    Creates a shortcut for a Git command (e.g., git config --global alias.co checkout).

7.3 Ignoring File Modes #

  • git config core.filemode false
    Ignores file mode changes.

8. Git Hooks #

8.1 Common Hooks #

  • pre-commit
    Runs before a commit is made.
  • pre-push
    Runs before a push is made.
  • post-merge
    Runs after a merge is completed.
  • post-checkout
    Runs after git checkout is used.

8.2 Managing Hooks #

  • .git/hooks
    Directory where hooks are stored.
  • chmod +x .git/hooks/<hookname>
    Makes a hook executable.

9. Collaboration & Workflows #

9.1 Forking & Pull Requests #

  • git fork
    Creates a fork of the repository on platforms like GitHub.
  • git pull-request
    Creates a pull request (often used with GitHub or GitLab).

9.2 Code Review #

  • git request-pull <start> <url> <end>
    Generates a request to review the code.

9.3 Working with Issues #

  • git checkout -b <branch> issue-<number>
    Creates a new branch for working on an issue.
  • git commit -m "Fixes #<issue-number>"
    Automatically closes an issue when pushed to a platform like GitHub.

10. Advanced Features #

10.1 Using Reflog #

  • git reflog
    Shows the history of changes to the tip of branches.
  • git reflog expire
    Removes older reflog entries.

10.2 Using Worktrees #

  • git worktree add <path> <branch>
    Adds a new working tree for a branch.
  • git worktree prune
    Removes worktrees that no longer exist.

10.3 Using LFS (Large File Storage) #

  • git lfs install
    Initializes Git LFS.
  • git lfs track "*.psd"
    Tracks large files with a specific extension.
  • git lfs push --all <remote>
    Pushes all LFS files to a remote repository.

This list includes a variety of commands that cover most use cases, but Git’s flexibility and power mean there are countless other commands and options that can be explored further depending on your needs.

What are your feelings
Updated on August 9, 2024