Git Commands Guide
This guide provides essential Git commands for repository management, branching, collaboration, and contributing via pull requests.
1. Create a Repository
Initialize a Local Repository
git initThis creates a new Git repository in the current directory.
Clone a Remote Repository
git clone <repository-url>Example:
git clone https://github.com/username/repository.git2. Branching
Create a New Branch
git branch <branch-name>Example:
git branch new-branchSwitch to a Branch
git checkout <branch-name>Example:
git checkout new-branchCreate and Switch to a New Branch
git checkout -b <branch-name>Example:
git checkout -b new-branch3. Pull Changes
Fetch and Merge Updates from the Remote Repository
git pullThis command pulls the latest changes from the default branch of the remote repository.
Pull from a Specific Branch
git pull origin <branch-name>Example:
git pull origin main4. Push Changes
Push a Branch to the Remote Repository
git push origin <branch-name>Example:
git push origin new-branchPush All Local Branches
git push --all5. Commit Changes
Add Changes to the Staging Area
git add <filename>Example:
git add index.htmlAdd All Changes
git add .Commit Staged Changes
git commit -m "<commit-message>"Example:
git commit -m "Update index.html structure"6. Making a Pull Request (PR)
Steps to Create a PR:
-
Fork the Repository (for contributing to someone else’s project):
Go to the repository page (e.g., GitHub) and click on the Fork button. -
Clone Your Forked Repository:
git clone <your-forked-repo-url> -
Create a New Branch:
git checkout -b <branch-name> -
Make Changes and Commit:
git add .git commit -m "Descriptive message about changes" -
Push Your Branch to Your Forked Repository:
git push origin <branch-name> -
Open a Pull Request:
- Go to the original repository page.
- Click on Pull Requests and then New Pull Request.
- Select your branch from your forked repository as the source and the original repository’s branch (e.g.,
main) as the target. - Add a title and description, and submit your pull request.
This guide provides a streamlined workflow for using Git effectively in collaborative and individual projects.