Git commands with examples

Category : GIT | Sub Category : GIT | By Prasad Bonam Last updated: 2023-07-29 12:32:15 Viewed : 66

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

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

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

    git clone
  3. git add <file>: Adds a file to the staging area, preparing it for the next commit. Example:

    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:

    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:

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

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

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

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

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

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

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

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

    git remote add origin
  14. git remote -v: Lists the remote repositories associated with your local repository. Example:

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

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

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

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

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

    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.

Related Articles

Leave a Comment: