GitHub is a widely used platform for version control and collaboration on software development projects. Whether you are a beginner or an experienced developer, understanding the most common GitHub commands is essential for efficient collaboration and effective project management. In this article, we will explore some of the most frequently used GitHub commands and their usages.
Table of Contents
Getting & Creating Projects:
- git init: Initializes a new Git repository in the current directory.
- git clone [URL]: Creates a local copy of a remote repository hosted on GitHub.
Basic Snapshotting:
- git status: Shows the status of your working directory, including untracked, modified, and staged files.
- git add [file-name.txt]: Adds a file to the staging area, preparing it for committing.
- git add -A: Adds all new and modified files in the working directory to the staging area.
- git commit -m “[commit message]“: Records changes in the staging area with a descriptive message.
- git rm -r [file-name.txt]: Removes a file (or folder) from the repository.
Branching & Merging:
- git branch: Lists all local branches (the asterisk denotes the current branch).
- git branch -a: Lists all branches (local and remote).
- git branch [branch name]: Creates a new branch.
- git branch -d [branch name]: Deletes a branch.
- git push origin –delete [branch name]: Deletes a remote branch.
- git checkout -b [branch name]: Creates a new branch and switches to it.
- git checkout -b [branch name] origin/[branch name]: Clones a remote branch and switches to it.
- git branch -m [old branch name] [new branch name]: Renames a local branch.
- git checkout [branch name]: Switches to a branch.
- git checkout –, Switches to the branch last checked out.
- git checkout — [file-name.txt]: Discard changes to a file.
- git merge [branch name]: Merges a branch into the active branch.
- git merge [target branch]: Merges a specific branch into another branch.
Additional Helpful Commands:
- git log: Shows the commit history of the repository.
- git diff: Shows the difference between files or commits.
- git pull: Updates your local branch with changes from the remote repository.
- git push: Pushes your local commits to the remote repository.
- git show [commit hash]: Shows the details of a specific commit.
- git reset: Undoes changes, either staged or committed.
- git stash: Temporarily saves changes away and then reapplies them when needed.
Here are some common Git commands with examples to walk you through using them:
1. Initializing a Repository:
- Command:
git init [repository-name]
- Example:
git init my-project
(This creates a new Git repository namedmy-project
in the current directory)
2. Adding Files to Staged Area:
- Command:
git add [file-name]
(adds specific files) - Example:
git add README.md
(adds theREADME.md
file to the staging area) - Command:
git add -A
(adds all new and modified files) - Example:
git add -A
(adds all changes in the project)
3. Committing Changes:
- Command:
git commit -m "[commit-message]"
- Example:
git commit -m "Added basic documentation"
(records all staged changes with the message)
4. Checking Status:
- Command:
git status
- Example:
git status
(shows untracked, modified, and staged files)
5. Viewing History:
- Command:
git log
- Example:
git log
(shows the commit history)
6. Branching & Merging:
- Command:
git branch [branch-name]
(creates a new branch) - Example:
git branch feature/new-feature
(creates a new branch namedfeature/new-feature
) - Command:
git checkout [branch-name]
(switches to a branch) - Example:
git checkout main
(switches to themain
branch) - Command:
git merge [branch-name]
(merges a branch into the current branch) - Example:
git merge feature/new-feature
(merges thefeature/new-feature
branch into the current branch)
7. Downloading from Remote:
- Command:
git clone [URL]
(clones a remote repository) - Example:
git clone https://github.com/user/repository.git
(downloads therepository
owned byuser
on GitHub) - Command:
git pull
(downloads changes from remote and merges them) - Example:
git pull origin main
(downloads changes from themain
branch of the remote repository namedorigin
and merges them into the current branch)
8. Pushing Changes to Remote:
- Command:
git push origin [branch-name]
(pushes changes to a remote branch) - Example:
git push origin main
(pushes changes from themain
branch to themain
branch of the remote repository namedorigin
)
These are just a few of the most commonly used GitHub commands. More options and details can be found in the Git documentation or with git help [command]
. By mastering these commands, you will be able to navigate and collaborate effectively on GitHub, improving your productivity as a developer.
Remember, practice is key to becoming proficient with these commands. Experiment with different scenarios and explore additional options and flags for each command to fully leverage the power of Git and GitHub in your development workflow.