Git commands with examples to demonstrate their usage

Category : GIT | Sub Category : GIT | By Prasad Bonam Last updated: 2023-07-29 15:36:59 Viewed : 69

Git commands with examples to demonstrate their usage:

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

  1. git init: Initializes a new Git repository in the current directory.
$ git init Initialized empty Git repository in /path/to/your/project/.git/
  1. git clone <repository-url>: Creates a copy of a remote repository on your local machine.
$ git clone Cloning into "example-repo"... ...
  1. git add <file>: Adds a file to the staging area, preparing it for the next commit.
$ git add myfile.txt
  1. git add . or git add --all: Adds all modified and new files in the current directory and its subdirectories to the staging area.
$ git add .
  1. git commit -m "Commit message": Commits the changes in the staging area to the repository with a descriptive message.
$ git commit -m "Added new feature XYZ"
  1. git status: Shows the status of your working directory, including which files are modified, staged, or untracked.
$ git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: myfile.txt
  1. git log: Displays the commit history, showing the latest commits first.
$ git log commit a1b2c3d4e5f6g7h8 (HEAD -> master) Author: John Doe <> Date: Tue Jul 27 12:34:56 2023 -0700 Added new feature XYZ commit b3c4d5e6f7g8h9a0 Author: Jane Smith <> Date: Mon Jul 26 09:12:34 2023 -0700 Fixed bug in previous version
  1. git branch: Lists all branches in the repository, with an asterisk indicating the currently active branch.
$ git branch feature-branch * master
  1. git checkout <branch-name>: Switches to the specified branch.
$ git checkout feature-branch Switched to branch "feature-branch"
  1. git checkout -b <new-branch-name>: Creates and switches to a new branch.
$ git checkout -b new-feature Switched to a new branch "new-feature"
  1. git pull: Fetches and merges changes from the remote repository into the current branch.
$ git pull origin master
  1. git push: Pushes committed changes from the local repository to the remote repository.
$ git push origin master
  1. git merge <branch-name>: Merges the specified branch into the currently active branch.
$ git merge feature-branch
  1. git remote add <remote-name> <remote-url>: Adds a remote repository with a given name and URL.
$ git remote add upstream
  1. git remote -v: Lists the remote repositories associated with your local repository.
$ git remote -v origin (fetch) origin (push)

These examples should give you an idea of how each Git command is used. Keep in mind that some commands may require additional arguments or options depending on the specific use case. Always refer to the official Git documentation for comprehensive information about each command.

Related Articles

Leave a Comment: