0% found this document useful (0 votes)
3 views

Revision+CheatSheet+Git+&+GitHub+Commands+

This document serves as a cheat sheet for getting started with Git, detailing essential commands for initializing repositories, making changes, branching, and working with remote repositories. It also highlights key differences between commands like git fetch vs git pull and git merge vs git rebase. Additionally, it encourages further exploration of Git's functionalities and collaboration within the GitHub ecosystem.

Uploaded by

webstertaifu44
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Revision+CheatSheet+Git+&+GitHub+Commands+

This document serves as a cheat sheet for getting started with Git, detailing essential commands for initializing repositories, making changes, branching, and working with remote repositories. It also highlights key differences between commands like git fetch vs git pull and git merge vs git rebase. Additionally, it encourages further exploration of Git's functionalities and collaboration within the GitHub ecosystem.

Uploaded by

webstertaifu44
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PhilosophersWhoCode.

org

Getting Started with Git


​ git init
● Use: Initializes a new Git repository in your current directory.
● When: At the beginning of a new project.
● Example: git init
● Explanation: This command creates a new subdirectory named .git that
houses all necessary repository files — a Git repository skeleton.
​ git clone <repo>
● Use: Copies an existing Git repository to your local machine.
● When: When starting work on an existing project.
● Example: git clone https://github.com/user/repository.git
● Explanation: This command makes a complete copy of the repository,
including all its history.

Make Changes
​ git status
● Use: Shows the status of changes as untracked, modified, or staged.
● When: Before and after making changes.
● Example: git status
● Explanation: Helps you understand what's happening in your repository.
​ git add <filename>
● Use: Adds specific files to the staging area.
● When: After making changes you wish to commit.
● Example: git add README.md
● Explanation: This command stages the changes you made to README.md.
​ git add . or git add -A
● Use: Adds all changed files to the staging area.
● When: When you have multiple files to stage.
● Example: git add .
● Explanation: Stages all modified and new (untracked) files.
​ git commit -m "Commit message"
● Use: Records staged changes in the repository.
● When: After staging changes.
● Example: git commit -m "Initial commit"
● Explanation: Saves your changes with a descriptive message.

Branching
​ git branch
● Use: Lists all local branches in the repository.
● When: To check or manage branches.
● Example: git branch
● Explanation: Displays all branches and highlights the current branch.
​ git branch <branchname>
● Use: Creates a new branch.
● When: When starting a new feature or fix.
● Example: git branch feature-x
● Explanation: Creates a new branch named feature-x.
​ git checkout <branchname>
● Use: Switches to another branch.
● When: To work on a different branch.
● Example: git checkout feature-x
● Explanation: Switches your working directory to the feature-x branch.
​ git merge <branchname>
● Use: Merges changes from one branch to another.
● When: To integrate changes from one branch to another (like master).
● Example: git merge feature-x
● Explanation: Merges changes from feature-x into the current branch.

Remote Repositories
​ git push origin <branchname>
● Use: Sends your commits to the remote repository.
● When: After committing your changes locally and ready to share.
● Example: git push origin master
● Explanation: Uploads your commits on the master branch to the remote
repository.
​ git pull
● Use: Fetches and merges changes from the remote repository to your
local repo.
● When: To update your local repository with changes from the remote.
● Example: git pull
● Explanation: Downloads changes and merges them into your local branch.
​ git remote -v
● Use: Lists all remote connections to other repositories.
● When: To view remote repositories.
● Example: git remote -v
● Explanation: Shows the URLs that Git has stored for the push and fetch
operations.

Key Differences
​ git fetch vs git pull
● git fetch downloads data from a remote repository but doesn't integrate
it into your working files. git pull does both: fetches and then merges.
​ git merge vs git rebase
● git merge combines the source and target branches via a new commit.
git rebase rewrites the project history by creating brand new commits for
each commit in the original branch.
​ git reset vs git revert
● git reset is used to undo local changes to the state of a Git repo, while
git revert is used to undo changes that are already committed to a
public repository. git revert keeps the project history, whereas git
reset can alter it.

Further Resources

This cheat sheet provides the foundational knowledge for Git usage. As you explore
more, you'll discover additional commands and nuances of Git.

To explore practical examples and use cases, particularly in a collaborative


environment. It's great for understanding how Git functions within the GitHub
ecosystem, including workflows and collaboration models.

● GitHub Docs on Git (docs.github.com).

You might also like