📘 Top 12 Git Commands Cheatsheet
git init - Initialize a new Git repository.
git clone - Clone a remote repository to your local machine.
git status - Check the current state of your working directory.
git add - Stage changes for the next commit.
git commit - Record staged changes and create a snapshot.
git push - Upload local changes to a remote repository.
git pull - Fetch and merge changes from a remote repository.
git branch - List, create, or delete branches.
git checkout / git switch - Switch between branches or commits.
git merge - Integrate changes from one branch into another.
git diff - View differences between working directory and staging area.
git log - Display a chronological list of commits.
### Basic Commands:
1. **Initialize a Repository:**
```bash
git init
```
2. **Clone a Repository:**
```bash
git clone <repository_url>
```
3. **Add Changes:**
```bash
git add <file(s)>
```
4. **Commit Changes:**
```bash
git commit -m "Commit message"
```
### Branching:
5. **Create a New Branch:**
```bash
git branch <branch_name>
```
6. **Switch to a Branch:**
```bash
git checkout <branch_name>
```
*(or use `git switch <branch_name>` in Git 2.23 and later)*
7. **Create and Switch to a New Branch:**
```bash
git checkout -b <new_branch_name>
```
*(or use `git switch -c <new_branch_name>` in Git 2.23 and later)*
8. **List Branches:**
```bash
git branch
```
### Merging:
9. **Merge Branch into Current Branch:**
```bash
git merge <branch_name>
```
### Remote Repositories:
10. **Add a Remote Repository:**
```bash
git remote add <remote_name> <repository_url>
```
11. **Fetch Changes from a Remote Repository:**
```bash
git fetch <remote_name>
```
12. **Pull Changes from a Remote Repository:**
```bash
git pull <remote_name> <branch_name>
```
13. **Push Changes to a Remote Repository:**
```bash
git push <remote_name> <branch_name>
```
### Undoing Changes:
14. **Discard Changes in Working Directory:**
```bash
git checkout -- <file(s)>
```
15. **Undo Last Commit (Keep Changes in Working Directory):**
```bash
git reset HEAD^
```
16. **Undo Last Commit (Discard Changes):**
```bash
git reset --hard HEAD^
```
### Logging and Status:
17. **View Changes:**
```bash
git status
```
18. **View Commit History:**
```bash
git log
```
### Miscellaneous:
19. **Configure Git:**
```bash
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
```
20. **Ignore Files:**
Create a `.gitignore` file and list files/directories to be ignored.
Remember to replace placeholders like `<repository_url>`, `<branch_name>`, `<remote_name>`, etc., with your actual values. This cheat sheet covers basic Git commands, and Git offers more advanced features that you may explore as needed.
Connect with me 😊
https://www.linkedin.com/in/imharry404/



0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home