Git commands with examples to demonstrate their usage

Category : GIT | Sub Category : GIT | By Prasad Bonam Last updated: 2023-07-29 10:06:59 Viewed : 344


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.
bash
$ 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.
bash
$ git clone https://github.com/exampleuser/example-repo.git Cloning into "example-repo"... ...
  1. git add <file>: Adds a file to the staging area, preparing it for the next commit.
bash
$ 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.
bash
$ git add .
  1. 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 XYZ"
  1. git status: Shows the status of your working directory, including which files are modified, staged, or untracked.
bash
$ 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.
bash
$ git log commit a1b2c3d4e5f6g7h8 (HEAD -> master) Author: John Doe <john@example.com> Date: Tue Jul 27 12:34:56 2023 -0700 Added new feature XYZ commit b3c4d5e6f7g8h9a0 Author: Jane Smith <jane@example.com> 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.
bash
$ git branch feature-branch * master
  1. git checkout <branch-name>: Switches to the specified branch.
bash
$ git checkout feature-branch Switched to branch "feature-branch"
  1. git checkout -b <new-branch-name>: Creates and switches to a new branch.
bash
$ 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.
bash
$ git pull origin master
  1. git push: Pushes committed changes from the local repository to the remote repository.
bash
$ git push origin master
  1. git merge <branch-name>: Merges the specified branch into the currently active branch.
bash
$ git merge feature-branch
  1. 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.git
  1. git remote -v: Lists the remote repositories associated with your local repository.
bash
$ git remote -v origin https://github.com/exampleuser/example-repo.git (fetch) origin https://github.com/exampleuser/example-repo.git (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.


Search
Sub-Categories
Related Articles

Leave a Comment: