Git has revolutionized the way developers collaborate and manage their codebases. From tracking changes to facilitating teamwork, Git offers a plethora of commands and functionalities to streamline the development process. In this guide, we'll explore essential Git commands and their functionalities to empower you in mastering version control.
Basic Operations
-
git init
: Initialize a new Git repository in the current directory. -
git clone <repository>
: Clone an existing repository to your local machine. -
git add <file>
: Stage changes in a specific file for the next commit.git add .
: Stage all changes in the current directory.
-
git commit -m "message"
: Commit staged changes with a descriptive message. -
git status
: Show the current status of the repository, including staged and unstaged changes. -
git log
: Display the commit history.git log --oneline
: Display the commit history in a compact format.
-
git diff
: Show differences between the working directory and the staging area or the last commit.git diff --staged
: Show differences between the staging area and the last commit.
Branching and Merging
-
git branch
: List all branches in the repository.git branch <branch-name>
: Create a new branch.git branch -d <branch-name>
: Delete a branch.
-
git checkout <branch-name>
: Switch to a different branch.git checkout -b <branch-name>
: Create a new branch and switch to it.
-
git merge <branch-name>
: Merge changes from a branch into the current branch. -
git pull
: Fetch changes from a remote repository and merge them into the current branch. -
git push
: Push local commits to a remote repository.git push -u <remote> <branch>
: Push the current branch to a remote repository and set the upstream.
Advanced Operations
-
git fetch
: Fetch changes from a remote repository without merging them. -
git stash
: Temporarily save uncommitted changes.git stash apply
: Apply the most recently stashed changes.
-
git remote
: List remote repositories.git remote add <name> <url>
: Add a new remote repository.
-
git revert <commit>
: Create a new commit that undoes the changes made in a specific commit, effectively reverting the repository to a previous state. -
git reset
: Reset the repository to a specific state.git reset --soft <commit>
: Move the branch pointer to the specified commit, keeping changes in the staging area.git reset --mixed <commit>
: Move the branch pointer to the specified commit, unstaging changes but keeping them in the working directory.git reset --hard <commit>
: Move the branch pointer to the specified commit, discarding all changes since that commit.
-
git cherry-pick <commit>
: Apply the changes introduced by a specific commit to the current branch. -
git rebase
: Reapply commits on top of another base branch.git rebase <branch>
: Rebase the current branch onto the specified branch.git rebase -i <commit>
: Interactively rebase commits, allowing for squashing, reordering, or editing.
-
git tag
: Create, list, or delete tags for specific commits.git tag <tag-name>
: Create a lightweight tag.git tag -a <tag-name> -m "message"
: Create an annotated tag with a message.git push --tags
: Push tags to a remote repository.
-
git blame <file>
: Show who last modified each line of a file and when. -
git show <commit>
: Display the details of a specific commit, including the changes made. -
git config
: Configure Git settings.git config --global user.name "Your Name"
: Set your name globally.git config --global user.email "your@email.com"
: Set your email globally.git config --global core.editor "vim"
: Set the default text editor.
-
git remote
: Manage remote repositories.git remote -v
: List remote repositories with their URLs.git remote remove <remote>
: Remove a remote repository.
-
git submodule
: Manage submodules within a repository.git submodule add <repository> <path>
: Add a submodule to the repository.git submodule update --init --recursive
: Initialize and update submodules recursively.
-
git reflog
: Display a log of all reference updates (branches, tags, etc.) in the repository. -
git bisect
: Use binary search to find the commit that introduced a bug.git bisect start
: Start the bisect process.git bisect good <commit>
: Mark a commit as good.git bisect bad <commit>
: Mark a commit as bad.