Basic Git and GitHub Commands
This document provides a guide to essential Git commands for version control and
collaboration using GitHub.
- Configuration
git config –global user.name “Your Name” : Sets your name for Git
commits globally. Replace “Your Name” with your actual name.
git config –global user.email “your.email@example.com” : Sets your
email address for Git commits globally. Replace “your.email@example.com”
with your actual email.
- Initialization & Cloning
git init : Initializes a new Git repository in the current directory.
git clone <repository_url> : Clones a remote repository to your local
machine. Replace <repository_url> with the URL of the repository.
- Staging & Committing
git status : Shows the status of your working directory, including modified and
staged files.
git add <file> : Stages a specific file for commit. Replace <file> with the
filename.
git add . : Stages all modified and untracked files in the current directory.
git commit -m “Commit message” : Commits the staged changes with a
descriptive message. Replace “Commit message” with your commit message.
- Branching & Merging
git branch : Lists all local branches in your repository.
git branch <branch_name> : Creates a new branch with the name
<branch_name> .
git checkout <branch_name> : Switches to the branch <branch_name> .git checkout -b <branch_name> : Creates a new branch named
<branch_name> and switches to it.
git merge <branch_name> : Merges the specified branch into the current branch.
git branch -d <branch_name> : Deletes the branch <branch_name> .
- Remote Repositories
git remote add origin <repository_url> : Adds a remote repository named
“origin” with the URL <repository_url> .
git remote -v : Lists the configured remote repositories.
git push origin <branch_name> : Pushes the local branch <branch_name> to
the remote repository “origin”.
git pull origin <branch_name> : Pulls changes from the remote branch
<branch_name> to your local branch.
git fetch : Retrieves the latest metadata from the remote repository without
merging any changes.
- Undoing Changes
git revert <commit_hash> : Creates a new commit that reverts the changes
made by the specified commit. Replace <commit_hash> with the hash of the
commit you want to revert.
git reset HEAD <file> : Unstages a file. Replace <file> with the filename.
git checkout — <file> : Discards changes to a specific file in your working
directory. Replace <file> with the filename.
- Inspecting History
git log : Shows the commit history.
git log –oneline : Shows the commit history in a compact, one-line format.
git diff : Shows the differences between your working directory and the last
commit.
git diff <file> : Shows the differences for a specific file.
git show <commit_hash> : Shows the details of a specific commit. Replace
<commit_hash> with the hash of the commit.
- Collaboration (GitHub Specific)Forking a Repository: Create a copy of a repository under your own GitHub
account.
Creating a Pull Request: Submit your changes from your forked repository to
the original repository.
Reviewing Pull Requests: Provide feedback and suggestions on pull requests
submitted by others.
- Merging Branches (Specific Example)
Here’s how to merge changes from dev_master to dev_preview :
git checkout dev_preview
git merge dev_master
This first switches to the dev_preview branch and then merges the changes from
dev_master into it. After merging, you’ll likely want to push the changes to the
remote repository:
git push origin dev_preview
Remember to resolve any merge conflicts that may arise during the merge process.