Git commands with examples

Category : GIT | Sub Category : GIT | By Prasad Bonam Last updated: 2023-07-29 07:02:15 Viewed : 323


Here are some Git commands with examples to demonstrate their usage:

  1. git init: Initializes a new Git repository in the current directory. Example:

    bash
    git init
  2. git clone <repository-url>: Creates a copy of a remote repository on your local machine. Example:

    bash
    git clone https://github.com/exampleuser/my-repo.git
  3. git add <file>: Adds a file to the staging area, preparing it for the next commit. Example:

    bash
    git add index.html
  4. git commit -m "Commit message": Commits the changes in the staging area to the repository with a descriptive message. Example:

    bash
    git commit -m "Added a new feature"
  5. git status: Shows the status of your working directory, including which files are modified, staged, or untracked. Example:

    bash
    git status
  6. git log: Displays the commit history, showing the latest commits first. Example:

    bash
    git log
  7. git branch: Lists all branches in the repository, with an asterisk indicating the currently active branch. Example:

    bash
    git branch
  8. git checkout <branch-name>: Switches to the specified branch. Example:

    bash
    git checkout main
  9. git checkout -b <new-branch-name>: Creates and switches to a new branch. Example:

    bash
    git checkout -b feature-branch
  10. git pull: Fetches and merges changes from the remote repository into the current branch. Example:

    bash
    git pull origin main
  11. git push: Pushes committed changes from the local repository to the remote repository. Example:

    bash
    git push origin feature-branch
  12. git merge <branch-name>: Merges the specified branch into the currently active branch. Example:

    bash
    git merge feature-branch
  13. git remote add <remote-name> <remote-url>: Adds a remote repository with a given name and URL. Example:

    bash
    git remote add origin https://github.com/exampleuser/my-repo.git
  14. git remote -v: Lists the remote repositories associated with your local repository. Example:

    bash
    git remote -v
  15. git fetch <remote-name>: Fetches changes from a remote repository without automatically merging them. Example:

    bash
    git fetch origin
  16. git diff: Shows the differences between the working directory and the staging area. Example:

    bash
    git diff
  17. git diff --staged: Shows the differences between the staged changes and the last commit. Example:

    bash
    git diff --staged
  18. git reset <file>: Removes a file from the staging area without undoing the changes. Example:

    bash
    git reset index.html
  19. git reset --hard: Discards all changes in the working directory and resets it to the last commit. Example:

    bash
    git reset --hard

These examples cover some common scenarios in Git, but there are many more commands and options available. Explore Git further and make use of its powerful features for version control and collaboration.

Search
Sub-Categories
Related Articles

Leave a Comment: