Comprehensive Guide to Git Commands
Git is an essential tool for version control in software development. It helps developers track changes in their code, collaborate efficiently, and manage multiple versions of a project. To maximize your Git workflow, it's crucial to familiarize yourself with its core commands. This article provides a comprehensive list of Git commands categorized for easier reference, from basic operations to advanced techniques.
Basic Git Commands
-
git init
Initialize a new Git repository in your project directory.
git init
-
git clone <repository-url>
Clone a remote repository to your local machine.
git clone https://github.com/user/repository.git
-
git status
Show the current state of the working directory and staging area (i.e., which files are staged, unstaged, or untracked).
git status
-
git add <file>
Add changes in a specific file to the staging area.
git add index.html
-
git commit -m "message"
Commit staged changes to the repository with a descriptive message.
git commit -m "Initial commit"
-
git log
View the commit history of the repository.
git log
-
git diff
Show the differences between the working directory and the index (staging area).
git diff
Branching and Merging Commands
-
git branch
List all branches in your repository.
git branch
-
git branch <branch-name>
Create a new branch.
git branch feature-xyz
-
git checkout <branch-name>
Switch to the specified branch.
git checkout feature-xyz
-
git merge <branch-name>
Merge the specified branch into the current branch.
git merge feature-xyz
-
git branch -d <branch-name>
Delete a local branch after it has been merged.
git branch -d feature-xyz
-
git merge --abort
Abort the merge process if conflicts arise and revert to the pre-merge state.
git merge --abort
Remote Repositories and Collaboration
-
git remote add <name> <url>
Add a new remote repository URL.
git remote add origin https://github.com/user/repository.git
-
git fetch
Fetch all branches from the remote repository, without merging them into your local branches.
git fetch origin
-
git pull
Fetch and merge changes from the remote repository into the current branch.
git pull origin main
-
git push
Push your local commits to a remote repository.
git push origin main
-
git push -u origin <branch-name>
Push a branch to the remote repository and set the upstream tracking branch.
git push -u origin feature-xyz
-
git remote -v
List the remote repositories associated with the current repository.
git remote -v
Undoing Changes
-
git reset <file>
Unstage a file, keeping the changes in the working directory.
git reset index.html
-
git reset --hard
Reset the repository to a specific commit and discard changes in the working directory.
git reset --hard HEAD
-
git revert <commit-hash>
Create a new commit that undoes the changes made by a specific commit.
git revert abc123
-
git checkout -- <file>
Discard changes in the working directory for a specific file, reverting it to the last committed state.
git checkout -- index.html
-
git stash
Temporarily store changes that are not yet ready to be committed.
git stash
-
git stash pop
Retrieve and apply the most recent stash.
git stash pop
Advanced Git Commands
-
git rebase <branch>
Rebase your current branch onto the given branch, integrating changes from the upstream branch.
git rebase main
-
git rebase --interactive
Launch an interactive rebase to edit, reorder, or squash commits.
git rebase -i HEAD~5
-
git cherry-pick <commit-hash>
Apply the changes introduced by an existing commit to the current branch.
git cherry-pick abc123
-
git bisect
Use binary search to find the commit that introduced a bug by checking out various commits between two points.
git bisect start
-
git reflog
Show the history of changes to theHEAD
reference, which helps recover lost commits.
git reflog
-
git filter-branch
Rewrite the commit history of a repository, useful for removing files or changing commit metadata.
git filter-branch --tree-filter 'rm -rf file' HEAD
Git Configuration and Setup
-
git config --global user.name "Your Name"
Set your username globally for Git commits.
git config --global user.name "Your Name"
-
git config --global user.email "youremail@example.com"
Set your email globally for Git commits.
git config --global user.email "youremail@example.com"
-
git config --list
List all the configuration settings for Git.
git config --list
-
git config --global core.editor <editor>
Set the default editor for commit messages and Git operations.
git config --global core.editor "vim"
Git Logs and History
-
git log
View the commit history with a detailed log of each commit.
git log
-
git log --oneline
View the commit history in a simplified format with one line per commit.
git log --oneline
-
git log --graph
View the commit history in a graphical representation.
git log --graph
-
git show <commit-hash>
Display detailed information about a specific commit.
git show abc123
Tagging in Git
-
git tag <tag-name>
Create a new tag at the current commit.
git tag v1.0
-
git tag -a <tag-name> -m "message"
Create an annotated tag with a message.
git tag -a v1.0 -m "Version 1.0 release"
-
git push origin <tag-name>
Push a specific tag to a remote repository.
git push origin v1.0
-
git push --tags
Push all tags to a remote repository.
git push --tags
Git Workflow and Management
-
git rm <file>
Remove a file from both the working directory and the staging area.
git rm index.html
-
git mv <old-filename> <new-filename>
Rename or move a file in your Git repository.
git mv oldfile.txt newfile.txt
Conclusion
This guide covers the most commonly used Git commands to help you work effectively with Git. From setting up your repository to handling advanced workflows, these commands provide the tools you need to manage your codebase, collaborate with others, and ensure smooth development processes.
By mastering these Git commands, you will be able to work efficiently on individual projects and in collaborative team environments, ensuring that version control is never a bottleneck in your workflow.
Top comments (0)