Here are 20 essential Git commands you should learn for effective use on GitHub
Git commands and their Explanations and examples:
- git config: This command sets up your Git user name and email, which will be associated with your commits.
- Example:
git config --global user.name "Your Name"
- Example:
git config --global user.email "[email protected]"
- git init: Initializes a new Git repository in your current directory.
- Example:
git init
(in your project directory)
- git clone: Creates a copy of a remote repository (hosted on GitHub) on your local machine.
- Example:
git clone https://github.com/username/repository.git
(replace with the specific repo URL)
- git add: Adds files to the staging area, which tells Git you want to include them in the next commit.
- Example:
git add filename.txt
(add a specific file) - Example:
git add .
(add all modified files)
- git commit: Creates a snapshot of the current state of your project with a descriptive message.
- Example:
git commit -m "Added new feature"
(commit with message)
- git diff: Shows the difference between your working directory and the staging area (uncommitted changes) or between the staging area and the latest commit (committed changes).
- Example:
git diff
(shows uncommitted changes) - Example:
git diff --cached
(shows changes staged for commit)
- git reset: Used to undo changes in your working directory or staging area.
- Be cautious! with these kinds of git commands, This can be dangerous if misused.
- git status: Shows the current status of your Git repository, including which files are modified, staged, or committed.
- Example:
git status
(get an overview of changes)
- git rm: Removes files from your project.
- Example:
git rm filename.txt
(remove a file) - Be cautious! With these kinds of git commands
- git log: Shows the commit history of your repository.
- Example:
git log
(view all commits)
- git show: Shows the details of a specific commit, including the commit message and changes made.
- Example:
git show commit_hash
(replace with the actual commit hash)
- git tag: Creates a tag, which is a lightweight reference to a specific commit.
- Example:
git tag v1.0.0 HEAD
(create a tag named v1.0.0 for the current commit)
- git branch: Manages branches in your Git repository. Branches allow you to work on different features or bug fixes independently.
- Example:
git branch new_feature
(create a new branch named new_feature) - Example:
git checkout new_feature
(switch to the new_feature branch)
- git checkout: Switches between branches in your repository.
- git merge: Combines changes from different branches into your current branch.
- Example:
git merge bugfix
(merge changes from the bugfix branch)
- git remote: Manages remote repositories (like the one on GitHub).
- Example:
git remote add origin https://github.com/username/repository.git
(add a remote named origin)
- git push: Uploads your local commits to the remote repository on GitHub.
- Example:
git push origin master
(push changes to the master branch on the remote repository)
- git pull: Downloads changes from the remote repository and merges them into your local branch.
- Example:
git pull origin master
(pull changes from the master branch on the remote repository)
- git stash: Temporarily saves your uncommitted changes away, allowing you to work on something else and come back to them later.
- Example:
git stash
(stash uncommitted changes) - Example:
git stash pop
(apply the most recent stashed changes)
- git clean: Removes untracked files from your working directory.
- Example:
git clean -f
(remove untracked files with force)
These 20 Git commands provide a solid foundation for working with Git on GitHub. Remember, practice is key! Feel free to experiment with these git commands on a local project to solidify your understanding.