Git is an essential tool for developers, providing version control for code repositories. Whether you’re new to coding or have been doing it for years, learning these Git commands will make your work easier and more efficient.
1. git clone
# Clone a repository from a remote URL to your local machine.
git clone <repository-url>
# Clone a specific branch of a repository.
git clone -b <branch-name> <repository-url>
# Clone a repository into a specific directory.
git clone <repository-url> <directory>
# Clone a repository with a limited commit history (shallow clone).
git clone --depth 1 <repository-url>
2. git status
# Shows the current status of files in the working directory and staging area.
git status
# Show a concise, symbolic status of changes (short format).
git status -s
3. git add
# Add a specific file to the staging area.
git add <file-path>
# Add all changes in the current directory to the staging area.
git add .
# Add all changes, including untracked files, to the staging area.
git add -A
4. git commit
# Commit the staged changes with a message.
git commit -m "Your commit message"
# Amend the previous commit with new changes and message.
git commit --amend -m "Updated commit message"
# Commit all changes (tracked and untracked) with a message.
git commit -a -m "Your commit message"
5. git push
# Push your local commits to the remote repository on the current branch.
git push
# Push your local commits to a specific remote and branch.
git push origin <branch-name>
# Force push your local commits to the remote repository (use with caution).
git push --force
6. git pull
# Pull the latest changes from the remote repository for the current branch.
git pull
# Pull the latest changes from a specific remote and branch.
git pull origin <branch-name>
7. git branch
# List all local branches.
git branch
# Create a new branch.
git branch <new-branch-name>
# Delete a local branch.
git branch -d <branch-name>
8. git checkout
# Switch to a specific branch.
git checkout <branch-name>
# Create a new branch and switch to it.
git checkout -b <new-branch-name>
# Restore a specific file from the latest commit.
git checkout -- <file-path>
9. git merge
# Merge a specific branch into the current branch.
git merge <branch-name>
# Merge a specific branch into the current branch, but keep the merge commits.
git merge --no-ff <branch-name>
# Merge a specific branch into the current branch, and resolve conflicts manually.
git merge <branch-name> --no-commit
# Abort a merge if there are conflicts or issues.
git merge --abort
9. git rebase
# Rebase the current branch onto the specified branch.
git rebase <branch-name>
# Rebase the current branch onto the latest commit of the specified branch and interactively resolve conflicts.
git rebase -i <branch-name>
# Continue the rebase after resolving conflicts.
git rebase --continue
# Abort the rebase operation and revert to the state before the rebase started.
git rebase --abort
# Rebase the current branch onto the specified branch and squash commits into a single commit.
git rebase -i --autosquash <branch-name>
Conclusion
These ten Git commands form the foundation of effective version control. As you become more comfortable with Git, you'll discover additional commands and workflows that further enhance your productivity. Happy coding!
Feel free to comment below with your favorite Git commands or tips!
Top comments (0)