The Ultimate Handbook of Git Commands
Unlocking the Power of Git: A Practical Guide to Essential Commands for Version Control Mastery
Here are some commonly used Git commands:
Initialize a new Git repository:
git init
initializes a new Git repository, setting up version control for your project by creating a hidden.git
directory to store history and configuration data.git init
Clone a remote repository:
git clone <repository_url>
clones an existing remote repository to your local machine, creating a local copy of the entire repository, including all branches and commit history.git clone <repository_url>
Check the status of the repository:
git status
shows the current status of your working directory, displaying any modified, staged, or untracked files.git status
Add changes to the staging area:
git add <file_name>
stages a specific file, preparing it for inclusion in the next commit.git add <file_name>
Add all changes to the staging area:
git add .
stages all changes in the working directory, preparing them for inclusion in the next commit.git add .
Commit changes to the local repository:
git commit -m "commit_message"
creates a new commit with the staged changes and a brief commit message describing the changes made.git commit -m "commit_message"
Push changes to a remote repository:
git push <remote_name> <branch_name>
pushes the committed changes from the specified branch to the remote repository.git push <remote_name> <branch_name>
Pull changes from a remote repository:
git pull <remote_name> <branch_name>
fetches and merges changes from the remote repository into your current branch.git pull <remote_name> <branch_name>
Create a new branch:
git branch <branch_name>
creates a new branch with the specified name.git branch <branch_name>
Switch to a different branch:
git checkout <branch_name>
switches to the specified branch, making it the active branch.git checkout <branch_name>
Create and switch to a new branch:
git checkout -b <branch_name>
creates and switches to a new branch with the specified name.git checkout -b <branch_name>
Merge a branch into the current branch:
git merge <branch_name>
merges the specified branch into the current branch, combining the changes from both branches.git merge <branch_name>
View the commit history:
git log
displays a detailed commit history, showing the latest commits first.git log
View the changes in a specific commit:
git show <commit_hash>
shows the details of a specific commit, including changes, author, date, and commit message.git show <commit_hash>
Discard changes in a file:
git checkout -- <file_name>
discards changes in the specified file and reverts it to the last committed state.git checkout -- <file_name>
Discard all local changes:
git reset --hard HEAD
discards all local changes and resets the working directory to the last committed state. Use with caution as it permanently deletes uncommitted changes.git reset --hard HEAD
Add a remote repository:
git remote add <remote_name> <repository_url>
adds a new remote repository with the specified name and URL to your Git project.git remote add <remote_name> <repository_url>
View remote repositories:
git remote -v
shows a list of remote repositories and their corresponding URLs.git remote -v
Remove a remote repository:
git remote remove <remote_name>
removes the specified remote repository from your Git project.git remote remove <remote_name>
Create a new tag:
git tag <tag_name>
creates a new lightweight tag with the specified name at the current commit.git tag <tag_name>
Push tags to a remote repository:
git push --tags
pushes all local tags to the remote repository.git push --tags
View a summarized commit history:
git log --oneline
displays a summarized commit history, showing each commit as a single line.git log --oneline
View a detailed commit history with graph:
git log --graph --oneline --all
displays a summarized commit history with a graph, showing all branches in a single line format.git log --graph --oneline --all
View the difference between two branches:
git diff <branch1> <branch2>
shows the difference between two branches, highlighting the changes made in each branch.git diff <branch1> <branch2>
Amend the last commit with new changes:
git commit --amend
allows you to amend the last commit by incorporating new changes or modifying the commit message.git commit --amend
Revert a specific commit and create a new commit:
git revert <commit_hash>
creates a new commit that undoes the changes introduced by the specified commit.git revert <commit_hash>
Interactively stage changes for a commit:
git add -p
interactively stages changes, allowing you to select portions of a file to add to the next commit.git add -p
Squash multiple commits into one:
git rebase -i HEAD~n
allows you to perform an interactive rebase, where you can modify, re-order, squash, or edit commits within the last "n" commits before the HEAD.git rebase -i HEAD~n
Rewrite commit history (use with caution):
git rebase -i <commit_hash>
enables interactive rebase, allowing you to modify, re-order, squash, or edit commits from the specified commit onwards.git rebase -i <commit_hash>
Create and apply a stash of changes:
git stash
temporarily saves changes.git stash apply
retrieves saved changes.git stash git stash apply
View stashed changes:
git stash list
provides a clear list of all saved stashes, showing their respective identifiers.git stash list
Cherry-pick a specific commit to the current branch:
git cherry-pick <commit_hash>
copies the changes introduced by the specified commit and applies them as a new commit on the current branch.git cherry-pick <commit_hash>
Show the commit history of a file:
git log -- <file_name>
shows the commit history for the specific file, displaying the changes made to that file over time.git log -- <file_name>
Create a new branch based on a specific commit:
git branch <branch_name> <commit_hash>
creates a new branch with the given name at the specified commit. The new branch will start from that commit's state in the commit history.git branch <branch_name> <commit_hash>
Update your local branch with the latest changes from the remote:
git fetch
retrieves new data from the remote repository without automatically merging it into your current branch.git fetch
Rebase your local branch on top of the latest changes from the remote:
git rebase origin/<branch_name>
integrates changes from the specified branch on the remote repository (origin) into your current local branch, replaying your local commits on top of the updated remote branch.git rebase origin/<branch_name>
Create an alias for a Git command:
git config --global alias.<alias_name> <git_command>
creates a global alias for a Git command, allowing you to use a custom shortcut (alias_name) for the specified Git command.git config --global alias.<alias_name> <git_command>
Ignore files or directories from being tracked by Git: Create a
.gitignore
file and list the files or directories to ignore.Configure global Git settings:
git config --global
user.name
"Your Name"
sets your global Git username.git config --global
user.email
"
your@email.com
"
sets your global Git email.git config --global user.name "Your Name" git config --global user.email "your@email.com"
Manage submodules (when working with nested repositories):
git submodule add <repository_url>
adds a new submodule to your repository.git submodule init
initializes all submodules that exist in your repository.git submodule update
updates all submodules to the latest commits.git submodule add <repository_url> git submodule init git submodule update
Cherry-pick changes from a specific file in another branch:
git checkout <branch_name> -- <file_name>
retrieves a specific file from the specified branch and replaces the local version of that file with the one from the branch.git checkout <branch_name> -- <file_name>
Conclusion:
This handbook of Git commands empowers you to navigate version control confidently, optimize your workflow, and streamline collaboration. Embrace the power of Git and revolutionize the way you build software. Happy coding!
Download cheatsheet: Cheat-sheet-link