GitHub Cheat Sheet: Basic Version Control with Git
GitHub Cheat Sheet: Basic Version Control with Git
This cheat sheet provides a quick reference for essential Git commands you'll use for version control on GitHub.
Setup:
- Install Git: Download and install Git from the official website (https://git-scm.com/downloads)
- Configure Git: Run
git config --global user.name "Your Name"andgit config --global user.email "your_email@example.com"to set your username and email for commits.
Working with Repositories:
- Clone an existing repository:
git clone https://github.com/<username>/<repository-name>.gitto create a local copy of a remote repository on GitHub. - Create a new local repository:
git initto initialize a new Git repository in your current directory.
Managing Changes:
- Track changes:
git add <filename>to add a specific file to the staging area (where files are prepared for commit). - Track a new folder:
git add <foldername>to add all files within a folder to the staging area. - Untrack a file:
git rm --cached <filename>to remove a file from the staging area (without deleting it from your working directory). - See changes:
git statusto view the status of your working directory and staging area (including modified, staged, and untracked files). - See detailed changes:
git diffto see the exact changes made to files (between working directory and staging area, or between commits).
Committing Changes:
- Commit changes:
git commit -m "<commit message>"to create a snapshot of your changes with a descriptive message.
Branching and Merging:
- List branches:
git branchto view all local branches. - Create a new branch:
git branch <branch-name>to create a new branch for development. - Switch branches:
git checkout <branch-name>to switch to a different branch. - Merge branches:
git merge <branch-name>to combine changes from another branch into your current branch (use with caution).
Remote Repositories (on GitHub):
- Add a remote repository:
git remote add origin https://github.com/<username>/<repository-name>.gitto link your local repository to the remote repository on GitHub (origin is a common alias for the remote). - Push changes to remote:
git push origin <branch-name>to upload your local commits to the specified branch on the remote repository. - Pull updates from remote:
git pull origin <branch-name>to download the latest changes from the remote repository and merge them into your local branch.
Additional Tips:
- Use meaningful commit messages that explain your changes.
- Regularly push your changes to the remote repository to create a backup and collaborate with others.
- Leverage Git branching for development and feature work.
- Explore Git's vast features for more advanced version control tasks.
Remember: This is a basic cheat sheet. Refer to the official Git documentation (https://git-scm.com/doc) for comprehensive details and advanced functionalities.
Comments
Post a Comment