Useful GitHub Command Every Developer should Know for Repository Management


Managing a GitHub repository efficiently is essential for developers working on collaborative projects or maintaining their codebase. Knowing the right GitHub commands can save time, streamline workflows, and prevent common issues. Whether you're just starting with GitHub or looking to improve your repository management skills, mastering these essential commands will help you navigate your projects with ease and confidence. From initializing repositories to handling merges and resolving conflicts, these commands are the backbone of effective GitHub usage.

Useful GitHub Command Every Developer should Know for Repository Management
Useful GitHub Command


Add Project to GitHub from Visual Studio Code

Step 1: Create a GitHub Repository

1. Go to GitHub: Log in to your GitHub account at github.com.

2. Create a New Repository:

  • Click on the + icon at the top right corner and select "New repository".
  • Fill in the repository name and description (optional).
  • Choose the visibility (public or private).
  • Click "Create repository".

Step 2: Open Your Project in VS Code.

1. Open Terminal: In VS Code, open a new terminal by going to Terminal > New Terminal.

2. Use this Commands:


git init

git add .

git commit -m "Initial commit"

git remote add origin <your-repository-URL>

git push -u origin main


Explanation of these commands

1. git init

  • Purpose: Initialize a new Git repository in your project directory.
  • What It Does: It creates a hidden .git folder in your project directory, which contains all the metadata and version history for your project.
  • Example: You have a folder named MyProject. Running git init inside this folder turns it into a Git repository.

2. git add. 

  • Purpose: Add all the changes in your project directory to the staging area.
  • What It Does: The . means all files and directories in the current directory. It stages all new, modified, and deleted files for the next commit.
  • Example: You created or modified files in your MyProject folder. Running git add . stages these changes.

3. git commit -m 'Initial commit'

  • Purpose: Records the staged changes in the repository with a message.
  • What It Does: Commits the changes that you have added to the staging area. The -m "Initial commit" part adds a commit message describing the changes.
  • Example: You want to save the changes with a description. Running this command commits the changes with the message "Initial commit".

4. git remote add origin https://github.com/yourusername/MyProject.git

  • Purpose: Connect your local repository to a remote repository on GitHub.
  • What It Does: This origin is the name you are giving to the remote repository. The URL https://github.com/yourusername/MyProject.git is the location of your repository on GitHub.
  • Example: You created a repository named MyProject on GitHub. Running this command links your local repository to this remote repository.

5. git push -u origin main

  • Purpose: Pushes your local commits to the remote repository.
  • What It Does: The -u flag sets the origin repository as the default for future pushes. main is the name of the branch you are pushing to. This command uploads your local commits to the main branch on GitHub.
  • Example: You have committed changes in your local repository. Running this command pushes these changes to the main branch of your remote repository on GitHub.

Examples Walkthrough

Let's say you have a project in a folder named 'MyProject'.

1. Create a GitHub Repository: You named the repository MyProject on GitHub.

2. Open VS Code: Now open the MyProject folder in VS Code.

3. Use these commands:


git init

git add .

git commit -m "Initial commit"

git remote add origin https://github.com/yourusername/MyProject.git

git push -u origin main


Your project is now on GitHub, and you can see it at https://github.com/yourusername/MyProject.

Update GitHub Repository from Visual Studio code

Update Commands

Use these commands to Update the Project in the GitHub Repository.


git add .

git commit -m 'description of changes'

git push origin main


Explaination of these Commands

1. git add .

  • Purpose: Add all the changed files in your project directory to the staging area.
  • What It Does: The . means all files and directories in the current directory. It stages all new, modified, and deleted files for the next commit.
  • Example: If you have made changes to your project files, running git add . stages these changes.

2. git commit -m "description of changes"

  • Purpose: Records the staged changes in the repository with a descriptive message.
  • What It Does: Commits the changes that you have added to the staging area. The -m "description of changes" part adds a commit message describing what you changed.
  • Example: You want to save the changes with a description. Running this command commits the changes with your specified message.

3. git push

  • Purpose: Pushes your local commits to the remote repository.
  • What It Does: Uploads your local commits to the remote repository on GitHub, updating the remote repository with your latest changes.
  • Example: You have committed changes in your local repository. Running this command pushes these changes to the corresponding branch of your remote repository on GitHub.

Example Walkthrough

Let's say you made some changes to your project files:

  1. Stage the Changes: Add the changed files to the staging area.
  2. Commit the Changes: Commit the staged changes with a descriptive message.
  3. Push the Changes: Push your local commits to the remote repository on GitHub.

git add index.html style.css app.js

git commit -m "Updated layout and fixed styles"

git push origin main


Pushing Specific Files to Github

Pushing specific files to GitHub involves adding only the files you want to update to the staging area, committing them, and then pushing the changes to the remote repository.

Pushing Specific Files Commands


git add file1 file2 file3

git commit -m "Add specific files"

git push origin main


Examples Walkthrough

Let's say you made changes to three specific files: index.html, style.css, and app.js.

  1. Stage the Specific Files: Add only these files to the staging area.
  2. Commit the Changes: Commit the staged changes with a descriptive message.
  3. Push the Changes: Push your local commits to the remote repository on GitHub.

git add index.html style.css app.js

git commit -m "Updated layout and fixed styles"

git push origin main

Removing Files from the GitHub Repository

Removing a file from a project and then updating the remote repository on GitHub involves a few simple steps. Here's how you can do it:

Removing Files Commands


rm <file-name>

git add .

git push 


Examples Walkthrough

Let's say you want to remove oldfile.txt from your project:

  1. Remove the File: Delete oldfile.txt from your project directory.
  2. Stage the Changes: Add the change (file removal) to the staging area.
  3. Commit the Changes: Commit the staged change with a descriptive message.

rm oldfile.txt

git add .

git commit -m "Removed oldfile.txt"

git push


Deleting a Directory or Files on GitHub but keeping it Locally

Deleting a directory or file from your GitHub repository but keeping it locally involves using Git to remove the file from version control without deleting it from your local filesystem. Here are the steps:

Removing GitHub Files Commands


git rm -r --cached <file-name>

git commit -m 'removed files'

git push origin main


Examples Walkthrough

Let's say you want to remove a file named config.yml from tracking:

  1. Remove from Tracking: Remove config.yml from Git's index but keep it locally.
  2. Commit the Change: Commit the staged change with a descriptive message.
  3. Push the Changes: Push your local commit to the main branch of the remote repository on GitHub.

git rm --cached config.yml

git commit -m 'removed config.yml from tracking'

git push origin main