Git commands with examples

Category : GIT | Sub Category : GIT | By Prasad Bonam Last updated: 2023-07-29 10:01:56 Viewed : 358


Here are some common Git commands with examples:

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

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

    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.

    bash
    $ git add index.html
  4. git add . or git add --all: Adds all modified and new files in the current directory and its subdirectories to the staging area.

    bash
    $ git add .
  5. git commit -m "Commit message": Commits the changes in the staging area to the repository with a descriptive message.

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

    bash
    $ git status
  7. git log: Displays the commit history, showing the latest commits first.

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

    bash
    $ git branch
  9. git checkout <branch-name>: Switches to the specified branch.

    bash
    $ git checkout development
  10. git checkout -b <new-branch-name>: Creates and switches to a new branch.

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

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

    bash
    $ git push origin master
  13. git merge <branch-name>: Merges the specified branch into the currently active branch.

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

    bash
    $ git remote add upstream https://github.com/upstream-repo/original-repo.git
  15. git remote -v: Lists the remote repositories associated with your local repository.

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

    bash
    $ git fetch upstream
  17. git diff: Shows the differences between the working directory and the staging area.

    bash
    $ git diff
  18. git diff --staged: Shows the differences between the staged changes and the last commit.

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

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

    bash
    $ git reset --hard HEAD

These examples should give you an idea of how to use these Git commands in various scenarios while working with a version-controlled project.

Search
Sub-Categories
Related Articles

Leave a Comment: