Skip to content
PotatoScript edited this page Mar 22, 2025 · 11 revisions

πŸ› οΈ BASIC GIT COMMANDS πŸš€

Git commands are instructions you type into the terminal/command prompt to interact with Git. These commands allow you to:

βœ… Create and manage repositories πŸ“‚
βœ… Add and commit changes πŸ“„
βœ… Push and pull changes from remote repositories πŸ”
βœ… Work with branches 🌿
βœ… Undo mistakes and restore previous versions βͺ


🎯 List of Basic Git Commands

Command Description
git init Initialize a new Git repository
git clone Copy (clone) an existing repository
git status Check the status of your working directory
git add Add files to the staging area
git commit Save changes to the local repository
git log View the commit history
git diff View differences between changes
git branch Manage branches
git checkout Switch between branches
git merge Merge changes from one branch to another
git pull Fetch and merge changes from a remote repository
git push Send changes to a remote repository
git remote Manage remote connections
git reset Undo changes and reset commit history
git stash Temporarily save changes without committing

πŸ“Œ Step 1: Initialize a New Git Repository

To create a new Git repository in your project folder:

git init

βœ… Output:

Initialized empty Git repository in /path/to/your/folder/.git/

πŸŽ‰ Success! Git is now tracking your project.


πŸ“Œ Step 2: Clone an Existing Repository

If you want to download a repository from GitHub, GitLab, or Bitbucket:

git clone https://github.com/username/repository-name.git

βœ… Example:

git clone https://github.com/example/MyProject.git

This creates a folder called MyProject and downloads all the repository content into it.


πŸ“Œ Step 3: Check the Status of Your Project

To see which files have been modified, staged, or untracked:

git status

βœ… Output:

On branch main
Untracked files:
  (use "git add <file>..." to include in what will be committed)
    README.md

This shows the current status of your files.


πŸ“Œ Step 4: Add Files to Staging Area

To add files to the staging area (preparing them for commit):

🟒 Add a Specific File

git add filename

βœ… Example:

git add README.md

🟒 Add All Files at Once

git add .

βœ… This adds all modified and new files.


πŸ“Œ Step 5: Commit Changes

A commit saves the staged changes to the local repository. Think of it as taking a snapshot of your project.

git commit -m "Your commit message"

βœ… Example:

git commit -m "Add initial project files"

πŸŽ‰ Your changes are now committed!


πŸ“Œ Step 6: View Commit History

To view the commit history of your project:

git log

βœ… Output:

commit abc1234567
Author: John Doe <johndoe@example.com>
Date:   Mon Jan 1 12:00:00 2025 +0000

    Add initial project files

🟒 View One-Line Summary

git log --oneline

βœ… Output:

abc1234 Initial commit
def5678 Add README file

πŸ“Œ Step 7: Check Differences Between Versions

To see the changes you’ve made compared to the last commit:

git diff

βœ… If you’ve staged the changes, compare staged changes with:

git diff --staged

πŸ“Œ Step 8: Create and Manage Branches

A branch is like a parallel universe where you can make changes without affecting the main project.

🟒 List All Branches

git branch

βœ… Output:

* main
  feature-branch

🟒 Create a New Branch

git branch branch-name

βœ… Example:

git branch feature-branch

🟒 Switch to a Branch

git checkout branch-name

βœ… Example:

git checkout feature-branch

🟒 Create and Switch to a New Branch

git checkout -b branch-name

βœ… Example:

git checkout -b new-feature

πŸ“Œ Step 9: Merge Branches

To merge changes from one branch into another:

git merge branch-name

βœ… Example:

git merge feature-branch

This applies the changes from feature-branch into the current branch.


πŸ“Œ Step 10: Push Changes to Remote Repository

To upload your changes to a remote repository:

git push origin branch-name

βœ… Example:

git push origin main

πŸŽ‰ Your changes are now on GitHub, GitLab, or Bitbucket! 🌐


πŸ“Œ Step 11: Pull Changes from Remote Repository

To fetch and merge changes from a remote repository:

git pull origin branch-name

βœ… Example:

git pull origin main

This keeps your local copy up to date.


πŸ“Œ Step 12: Reset or Undo Changes

Sometimes, you may want to undo your changes.

🟒 Unstage Files

To remove files from the staging area:

git restore --staged filename

βœ… Example:

git restore --staged README.md

🟒 Undo Local Changes

To discard all local changes:

git restore filename

🟒 Reset Last Commit

git reset --hard HEAD~1

This removes the last commit entirely. Be careful! ⚠️


πŸ“Œ Step 13: Stash Your Work Temporarily

If you need to save your work temporarily without committing:

git stash

βœ… When you’re ready to restore your work:

git stash pop

πŸ“Œ Step 14: Manage Remote Repositories

To view your current remote repositories:

git remote -v

🟒 Add a Remote Repository

git remote add origin https://github.com/username/repo.git

🟒 Change Remote URL

git remote set-url origin https://github.com/username/new-repo.git

  • use $ ls to list out the project folder that you want to track on

  • next cd to the folder

  • create a directory for our project
    mkdir myproject and then cd to the directory cd myproject

  • after you decided the location of the project use init to create a git folder in your project
    $ git init to initializing empty Git repository in our project directory
    NOTE: the .git file that create by init was hidden file and you should not do anything on it.
    It will store all the history of our project .

  • to list all the file in the folder
    $ ls for short list without hidden file
    $ ls -a for short list with hidden file
    $ ls -la for full detail list with hidden file

  • to see the content of a file $ cat file.txt

  • to make your git command look beautiful you can download posh-git for window

  • to remove the folder for tracking on the project use
    $ rm -rf .git

  • Ignoring Directory or File
    Before commit we need to decide which file to be send to the repositories and which wont
    a. Create a gitignore file via $ touch .gitignore or

    $ echo logs/ > .gitignore this will create a .gitignore file with logs/ content
    - use code .gitignore to open the .gitignore in our editor and add the directory and file to be ignored example: logs/ *.log
    b. If you add and commit the directory before adding it to the .gitignore its wouldn't ignore it

    • to solve this problem we need to remove this directory from the staging area
    • let list out all the files in the staging area using $ git ls-files
      $ git rm --cached -r <files> or <directory> this will only remove the directory or file from the staging are only not working dir
    • let check at https://github.com/github/gitignore to check the gitignore file for difference programming language.
      c. you can get the template of the .gitignore at
      https://www.codegrepper.com/code-examples/csharp/asp.net+core+.gitignore
  • Add the file or folder that do not wish to be send to the repositories in the .gitignore file
    *.pyc this will ignore all the .pyc extension file

  • Add file to the directory or project

    1. $ echo "some content here" > file1.txt or
    2. $ touch file1.txt without adding any content to it
  • To add more content to the file we use >> in the echo
    $ echo "new line of content" >> file1.txt

  • There are three area
    a. Working Directory - Stage Fixes
    b. Staging Area - Commit
    c. .git directory (Repository) - Checkout the project

  • To View the status
    a. $ git status //for full status
    b. $ git status -s //for short status

  • To add your file to the Staging Area use add
    a. $ git add <file> to add individual file one by one

    b. $ git add -A to add all the file at one time
    c. $ git add file.js file2.js to staging multiple files
    d. $ git add . to staging the current directory and all its content
    e. $ git add *.js to staging with a pattern

  • To remove the file from the Staging Area use reset
    a. use $ git reset <file> to reset the particular file
    b. use $ git reset to reset all files from the Staging Area
    c. or use $ rm <file> only remove from the origin not at staging area

    d. use $ git rm <file> can remove the file from the origin and staging area

  • Next Commit to .git directory = Now we have a snapshot in the staging area ready to be permanently stored in our repository
    a. $ git commit -m "some message here"
    b. you can use $ git log to see the commit summary information
    c. If short one-linear description is not sufficient
    you want to explain some details to give context for example if you worked on a bug
    In situations like this we drop the message and just type $ git commit and press enter this opens our default editor.
    A short description should be less than 80 characters then we add a line break
    And follow by Long description

  • you can use $ git diff to see the change on the code you had done

  • you can skip the staging step as well only if you know what you are doing if you are 100% sure that your code
    your change don't need to be reviewed because that's the whole point of having a staging area
    $ git commit -a -m "message here" you are skipping $ git add -a or
    $ git commit -am "message here"

  • move and rename the file
    $ mv file1.txt main.js the file1.js was renamed to main.js
    next we need to staging the change
    $ git add file1.txt and $ git add main.js

    • if we use $ git mv file1.txt main.js we don't need to run $ git add file1.txt and $ git add main.js again

Clone this wiki locally