An open API service indexing awesome lists of open source software.

https://github.com/akashdip2001/git-github

All important commands using in git-GitHub || solve merge conflict between Vs-Code & GitHub-web-editor
https://github.com/akashdip2001/git-github

git git-github-vscode-setup github github-cloud-migrated github-config merge-conflict vscode vscode-git vscode-github

Last synced: 7 months ago
JSON representation

All important commands using in git-GitHub || solve merge conflict between Vs-Code & GitHub-web-editor

Awesome Lists containing this project

README

          

github_logo

# Git & GitHub: Essential Commands

| πŸ”­ [**my website**](https://akashdip2001.github.io/linktree/) | [Git docs.](https://docs.chaicode.com/git-and-github/) | [Exams MCQ](./Exam/1%20github%20foundations%20certification/readme.md) |
| --- | --- | --- |

| [ My YouTube Channel](https://www.youtube.com/c/akashaot) |
| --- |

---






Β  Β 

more Badges πŸ₯‡







---

## **Table of Contents**

python_logo

0. [linux-basics-course](https://github.com/akashdip2001/linux-basics-course)
1. [Install Git & VS Code](#install-git--vs-code)
2. [Check Git Installation](#check-git-installation)
3. [Configuring Git (Terminal)](#configuring-git-terminal)
- [UserName]()
- [Email]()
4. [Create Repository](#create-and-push-local-repository)
- [Create Local Repository](#create-and-push-local-repository)
- [Clone & Check Status](#clone-Repository)
- [Git Status Indicators](#status-indicators)
5. [Add and Commit (Local)](#add-and-commit-local)
- [Create file]()
- [git add .]()
- [git commit]()
- [Then Modified & commit]()
6. [Push to Remote Repository on GitHub](#push-upload-to-remote-repository-on-github)
1. [VS-code & Web-editor ⚠️ Merge Conflicts](https://youtu.be/vArkGr5nzVU)
2. πŸš€ [Update the Git Remote URL]()
8. [Initialize a New Repository](#initialize-a-new-repository)
9. [Branch Commands](#git-branch-commands)
- [Check the Branch]()
- [Rename a Branch]()
- [Create a New Branch]()
- [Check all existing Branches]()
- [Navigate Between Branches]()
- [Delete a Branch]()
- πŸ”΅ [practical Problems](#practical-Problems)
10. [Merging Code](#git-merging-code)
11. [GitHub Exam](./Exam/1%20github%20foundations%20certification/readme.md)

---

## Install Git & VS Code

### Just copy & paste the code into your PowerShell - Done βœ”οΈ

You can install Git Bash using Chocolatey, a package manager for Windows. If you haven't installed Chocolatey yet, run the following command in PowerShell with administrative privileges:

```powershell
Set-ExecutionPolicy Bypass -Scope Process -Force; `
[System.Net.ServicePointManager]::SecurityProtocol = `
[System.Net.ServicePointManager]::SecurityProtocol -bor 3072; `
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
```

Then, install Git and VS Code:

```powershell
choco install git.install
choco install vscode
```

---

## Check Git Installation

python_logo

```bash
git --version
```
```bash
#check where oyu are?
pwd
```
```bash
#All files & folders
ls
```
```bash
#The folder tracked by git or not ?
git status
```

| [docs](https://docs.chaicode.com/terminology/) |
| --- |

---

## Configuring Git (Terminal)

```bash
git config --global user.name "Your Name"
```
```bash
git config --global user.email "your.email@example.com"
```
```bash
git config --list
```

![Git Configuration](https://user-images.githubusercontent.com/73097560/115834477-dbab4500-a447-11eb-908a-139a6edaec5c.gif)

# πŸ¦– Create an Repository Locally, Edit, and Push from GitHub

python_logo

```yaml
Main Folder
β”‚
│── Folder 01
└── Folder 02: I want to track this folder
```
```bash
cd Folder 02
```
```bash
git status
```
```bash
git init
```
```yaml
Py Projects
Main Folder
β”‚
│── Folder 01
└── Folder 02
β”‚
└── .git: ⚠️
```
## ⚠️ Not showing .git Folder ?
#### Open Vs code -> Settings -> Exclud -> Remove Git from here.


image Guide



Image 1
Image 2


![Git Configuration](https://user-images.githubusercontent.com/73097560/115834477-dbab4500-a447-11eb-908a-139a6edaec5c.gif)

# Or, πŸ¦– Clone an Existing Repository Locally, Edit, and Push from GitHub

---

## Clone and Check Status

```bash
git clone https://github.com/username/repository.git
```

```bash
git status
```

### Status Indicators

Here’s a comprehensive table summarizing the states and their short indicators in Git, along with descriptions:

| **State** | **Short Indicator** | **Description** | **Icon** |
|-------------------|---------------------|-----------------------------------------------------------------------------------------------------|--------------------------------|
| **Untracked** | `??` | Files that Git doesn't track yet. They are new and need to be added `git add .` to start tracking. | *(No specific icon provided)* |
| **Modified** | `M` | Files that have been changed but are not yet staged for commit. | |
| **Staged** | `A` or `M` | Files added to the staging area. `A` for newly added, `M` for modified files staged for commit. | |
| **Unmodified** | *(none)* | Files that are tracked and have not been modified; they remain in their last committed state. | *(No specific icon provided)* |
| **Unmerged** | `U` (or `UU`) | Files with conflicts during a merge; need resolution before completing the merge. | |

- `UU` for a file that is unmerged with conflicts.
- `AA`, `DD`, etc., for specific merge conflict cases (e.g., both sides added or deleted a file).

## Add and Commit (Local)
python_logo

### ✈️ Create new file --> add & commit

```bash
touch "file 01" "file 02"
```
```yaml
Py Projects
Main Folder
β”‚
│── Folder 01
└── Folder 02
β”‚
│── .git:
│── file 01
└── file 02
```
```bash
# git add filename_01.txt filename_02.txt
git add .
```

New file add .


Again create a New file & commit all

*Use `git add .` to add all changes.*

```bash
git commit -m "1st commit"
```
---

### ✈️ Modified (edit) any file --> **again** add & Commit again


Image 1
Image 2


Next img


*Again Use `git add .` to add all changes.*
```bash
git add filename_02.txt
```

```bash
git commit -m "2nd commet after modified a file"
```



## Push (Upload to Remote Repository on GitHub)

[python_logo](https://www.youtube.com/watch?v=BXR_HoROl3E)


⚠️⚠️⚠️ VS-code & Web-editor ❌ Merge Conflicts ⚠️⚠️⚠️

[![git & github with vs code setup](https://github.com/user-attachments/assets/a760cf72-1732-4ec3-b01f-f45112001928)](https://www.youtube.com/watch?v=mryez-QqMj4)

### ⚠️ I have a github propository in the github. And I clone this repository using http URL, so I directly push my all changes through VS code. But what happened eat I open Github web editor and modify any file and push from it. This time how to sync my geethab with vs code in this two cases - case1 - if my I not work on vs good and there was no modified or changes. And in case2 - what happened I update from GitHub web editor but there was also some modification exist in v.s code which is comet but not push ⚠️

### βœ… Solution βœ…

To synchronize your local repository (in VS Code) with the remote GitHub repository in both cases, follow these steps:

---

### βœ… **Case 1: No Local Changes in VS Code**
You updated a file in the GitHub web editor, but no changes were made in your local repository.

1. **Pull the Latest Changes**:
- Open the terminal in VS Code or use the Source Control tab.
- Run:
```bash
git pull origin
```
- Replace `` with the branch you are working on (e.g., `main` or `master`).

2. **Confirm Sync**:
- After pulling, your local repository will now match the remote repository since there were no local changes to cause a conflict.

---

### βœ… **Case 2: Local Changes in VS Code Not Pushed Yet**
You updated a file in the GitHub web editor and also made changes locally that are committed but not pushed.

1. **Pull the Latest Changes**:
- First, fetch and merge the remote changes using:
```bash
git pull origin
```

2. ⚠️ **Resolve Merge Conflicts (if any)**:
- If there are conflicts between the changes made locally and those from the GitHub web editor, Git will indicate the files with conflicts. Resolve these conflicts by:
- Opening the conflicted files in VS Code.
- Looking for conflict markers (`<<<<<<<`, `=======`, `>>>>>>>`).
- Deciding whether to keep your changes, the remote changes, or both.
- After resolving the conflicts, stage the resolved files:
```bash
git add
```
- Commit the resolved changes:
```bash
git commit -m "Resolved merge conflicts"
```

3. **Push Your Changes**:
- Once the merge is complete and there are no conflicts, push your changes back to the remote repository:
```bash
git push origin
```

---

### **Notes**
- Always pull the latest changes before starting work in VS Code to minimize conflicts.
- Use `git status` frequently to check the state of your repository and understand whether changes are staged, unstaged, or committed.
- If you only want to review the changes made in the GitHub web editor before pulling, use:
```bash
git fetch origin
git log origin/
```
[![git & github with vs code setup](https://github.com/user-attachments/assets/7c27723a-a05c-4c29-ba4a-18d81c225eb2)](https://www.youtube.com/watch?v=vArkGr5nzVU)

#### βœ… This approach keeps both your local and remote repositories in sync efficiently. βœ…

### Push Changes to a Remote Repository

Once you've committed your changes locally, use the `git push` command to push those changes to a remote repository.

**Syntax:**

```bash
git push
```

**Example:**

```bash
git push origin main
```

*Replace `origin` with the name of your remote repository, and `main` with the name of the branch you're pushing to.*

If it's your first time pushing to the remote repository, you may need to set up tracking:

```bash
git push -u origin main
```

*After the initial setup, simply use `git push` for future pushes.*

```bash
git push
```

πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯

## Update the Git Remote URL

πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯
### 1. πŸš€ git clone from a another profile/repo
![Screenshot (708)](https://github.com/user-attachments/assets/fb5aa6d3-25e5-4afd-a9f0-376ca1cf6f6e)

### 2. πŸš€ Check the Targate repo
![Screenshot (709)](https://github.com/user-attachments/assets/5a017d80-ea69-4291-ada5-8f5de91c04fa)

### 3. πŸš€ Update the Targate repo

#### Step 1: Update the Git Remote URL
In the `node-todo-cicd` directory, update the remote URL to point to your repository:

```bash
git remote set-url origin https://github.com/akashdip2001/AWS.git
```

Verify the change with:

```bash
git remote -v
```

It should now display:

```plaintext
origin https://github.com/akashdip2001/AWS.git (fetch)
origin https://github.com/akashdip2001/AWS.git (push)
```

---

### Step 2: Move the Files to the Target Directory
1. Create the directory structure in your repository:

```bash
mkdir -p Projects/CI-CD/Project\ 001
mv * Projects/CI-CD/Project\ 001/
```

2. Ensure no unnecessary files (e.g., `.git` from the original project) are moved:

```bash
rm -rf Projects/CI-CD/Project\ 001/.git
```

---

### Step 3: Stage and Commit the Changes
1. Add the new directory and its content to the staging area:

```bash
git add Projects/CI-CD/Project\ 001/
```

2. Commit the changes with an appropriate message:

```bash
git commit -m "Added node-todo-cicd project to Projects/CI-CD/Project 001"
```

---

### Step 4: Push the Changes
Push the changes to your repository on GitHub:

```bash
git push origin master
```

Replace `master` with the branch name you're using if it differs.

---

### Step 5: Verify the Upload
1. Go to your repository on GitHub: [https://github.com/akashdip2001/AWS](https://github.com/akashdip2001/AWS).
2. Navigate to `Projects/CI-CD/Project 001` to ensure the files are correctly placed.

---

This method ensures the project is correctly uploaded to the desired directory in your repository.

![Screenshot 2024-12-20 172351](https://github.com/user-attachments/assets/af095196-abe7-4d31-9b62-db7c7a3a37bd)

πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯πŸš₯
---
---

## Initialize a New Repository

**`init`** - Used to create a new repository

```bash
mkdir
cd
git init
```

*Use `cd ..` to navigate back to the main folder.*

*`mkdir` creates a new folder and `cd` enters it. Then, create or edit files within the folder.*

```bash
git status
git add .
git commit -m "Initial commit"
git status
```

```bash
git push origin main
```

![Git Initialization](https://user-images.githubusercontent.com/73097560/115834477-dbab4500-a447-11eb-908a-139a6edaec5c.gif)

# πŸ¦– Creating a Repository on GitHub (Without README) & Uploading Local Files

**Without a README**: This allows you to clone the repository locally, initialize Git, and push all local files.

```bash
git remote add origin https://github.com/username/repository.git
git remote -v
```

*Use `git remote -v` to verify the remote connection.*

## Check the Branch

```bash
git branch
```

*If you're on the `master` branch, rename it to `main`.*

### Rename the Branch to `main`

```bash
git branch -M main
git push -u origin main
```

*The `-u` flag sets up tracking for future pushes, allowing you to use `git push` without specifying the remote and branch.*

## Push Any Changes Locally

```bash
git status
git add .
git commit -m "Add new file or update"
git push
```

# πŸ¦– Branch Commands



### ✈️ Check the Branch

```bash
git branch
```

### ✈️ Rename a Branch

```bash
#git branch -M main
git branch -m
```

### ✈️ Create a New Branch

```bash
git branch
```
```bash
git checkout -b
```

### ✈️ Check all existing Branches
```bash
git branch
```

also check using HEAD file

### ✈️ Navigate Between Branches
```bash
# create new Branch Then switch
git branch
```
```bash
git switch
```
```bash
git checkout
```
```bash
# Auto create the branct (if not exist) & switch
git switch -c
```

### ✈️ Delete a Branch

```bash
git branch -d
```

### βœ… After Making Changes

```bash
git status
git add .
git commit -m "Your commit message"
git push origin
```
| [docs](https://docs.chaicode.com/branches-in-git/) |
| --- |

## Create a new Branch in VS-Code ⚠️ git push


steps

![Screenshot (359)](https://github.com/user-attachments/assets/e1d6f25d-6485-4044-b3c3-586d65a1fb95)

```bash
git branch
```
```bash
git branch "Test-Share-link"
```
```bash
git switch "Test-Share-link"
```
```bash
git branch
```
```bash
git status
```
```bash
git add .
git commit -m "add Test-Share-link .md file"
# ❌ git push
```
```bash
# βœ…
git push --set-upstream origin Test-Share-link
```
![Screenshot (360)](https://github.com/user-attachments/assets/07bde987-8385-4313-9b14-033510b007c6)
![Screenshot (361)](https://github.com/user-attachments/assets/883e57e7-16df-4fae-babf-70b8a0d23eab)
![Screenshot (362)](https://github.com/user-attachments/assets/c9e4e990-6403-4e1c-9325-8a9e0b2bea1e)
![Screenshot (363)](https://github.com/user-attachments/assets/596f4a09-f6cf-4dd6-b641-ad631494d859)

![Branch Operations](https://user-images.githubusercontent.com/73097560/115834477-dbab4500-a447-11eb-908a-139a6edaec5c.gif)

---

# Some practical-Problems
## create a Project Locallyt with branch name `master` and πŸ”΅ want to upload a existing Repo with `main' branch.
### 🟒 Normally you can push your code with `master` and Marge the Branch in GitHub βš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈ
> *β€œThe workspace currently open doesn’t have any folders containing Git repositories.”*

You want to upload this directory to your **existing GitHub repo**:
πŸ‘‰ [`https://github.com/akashdip2001/Arduino-IDE-setup`](https://github.com/akashdip2001/Arduino-IDE-setup)

---

### βœ… Steps to Upload Your Current Directory to That Repo:

Follow these simple steps inside VS Code terminal:

---

#### 🟒 1. Initialize Git in Your Project Folder

If you haven’t yet:
```bash
cd path/to/ESP8266_LED_Control
git init
```

---

#### 🟒 2. Add Remote Link to Your Existing GitHub Repo

```bash
git remote add origin https://github.com/akashdip2001/Arduino-IDE-setup.git
```

> If you get an error like *β€œremote origin already exists”*, use:
```bash
git remote set-url origin https://github.com/akashdip2001/Arduino-IDE-setup.git
```

---

#### 🟒 3. Add and Commit All Files

```bash
git add .
git commit -m "Upload ESP8266 LED Control project"
```

---

#### 🟒 4. Push to GitHub

> If your GitHub repo's branch is `main`:

```bash
git push -u origin main
```

> Or if it's `master`:

```bash
git push -u origin master
```

---

#### βœ… Done! Check your repo

Go to your repo page: [github.com/akashdip2001/Arduino-IDE-setup](https://github.com/akashdip2001/Arduino-IDE-setup) β€” your files should now be uploaded πŸŽ‰

---

## πŸ”΅βš οΈ Some Tile error comes when try to upload in `main` branch
you **can directly upload to the `main` branch**, but here’s what’s happening and how to fix it in the cleanest way:

---

### πŸ” What Happened ??

- Your **local branch** is named `master` by default.
- Your **GitHub repo (`https://github.com/akashdip2001/Arduino-IDE-setup.git`)** has the **`main` branch** (not `master`).
- When you tried `git push origin main`, Git said:
`error: src refspec main does not match any`
➀ Because your local branch is still named `master`.

---

### βœ… Two Easy Fix Options

#### πŸ”΅ **Option 1: Rename Local `master` to `main`** (RECOMMENDED)

This way, you’ll align with GitHub’s default and won’t create extra branches.

```bash
git branch -m master main
git push -u origin main
```

Done! Now you’re working with the `main` branch directly, just like your GitHub repo. βœ”οΈ

---

#### πŸ”΅ **Option 2: Push `master` as `main`**

If you don't want to rename the local branch but still push it to `main` on GitHub:

```bash
git push -u origin master:main
```

This tells Git: β€œPush my local `master` branch **to the remote `main` branch**.”

---

### 🧹 Clean Tip

To always start your new projects with a `main` branch locally, you can set this globally:

```bash
git config --global init.defaultBranch main
```

This avoids the `master` vs `main` mix-up in the future. βœ…

---

## ⚠️ error come for **Option 1**
This is just a small sync issue between your **local `main`** branch and the **remote `main`** branch on GitHub.

---

### πŸ’₯ What's the problem ??

The remote `main` branch **already has some commits** (probably a README or `.gitignore` added through GitHub's UI), but your local `main` doesn't have them. So Git is blocking your push to avoid overwriting remote history.

---

### βœ… How to fix it safely

Here’s the step-by-step:

---

### πŸ”΅
1. **Pull remote `main` first** and merge

```bash
git pull origin main --allow-unrelated-histories
```

- The `--allow-unrelated-histories` flag helps when your local repo and the GitHub repo were initialized separately.
- You may be asked to enter a merge commit message. Just accept it (save and close the editor).

---

### 2. **Now push successfully**

```bash
git push -u origin main
```

Boom πŸ’₯! Now your local project is successfully pushed to GitHub’s `main` branch.

![Screenshot (292)](https://github.com/user-attachments/assets/185d6f24-9799-4b82-9070-2dfddd3c5630)

---

### βœ… Optional Cleanup (Next time)

To avoid this in future projects:

- **Create the repo on GitHub *without* any files** (uncheck README, .gitignore, License).
- Or **initialize your local repo first**, then push, and GitHub won’t have conflicts.

---

βš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈβš™οΈ

# πŸ¦– Merging Code

## Method 1: Using Git Commands

python_logo

```bash
#git diff # Compare commits, branches, files & more
git merge
```


GUI

## Method 2: Creating a Pull Request (PR)

```bash
git pull origin main # Download and synchronize with GitHub
```

![Merge Operations](https://user-images.githubusercontent.com/73097560/115834477-dbab4500-a447-11eb-908a-139a6edaec5c.gif)

---

| πŸ“„ [Documentation](https://docs.chaicode.com/git-and-github/) | [My YouTube Channel](https://youtu.be/q8EevlEpQ2A?si=AFDUT-cTa-OXjufz&t=6370) |
| --- | --- |

---

# [GitHub Exam](./Exam/1%20github%20foundations%20certification/readme.md)

[![github exam](https://github.com/user-attachments/assets/61d30632-d31c-4365-b015-38126371dbef)](https://github.com/akashdip2001/git-github/tree/main/Exam)