Category : GIT | Sub Category : GIT | By Prasad Bonam Last updated: 2023-07-29 10:01:56 Viewed : 785
Here are some common Git commands with examples:
git init
: Initializes a new Git repository in the current directory.
bash$ git init
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
git add <file>
: Adds a file to the staging area, preparing it for the next commit.
bash$ git add index.html
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 .
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"
git status
: Shows the status of your working directory, including which files are modified, staged, or untracked.
bash$ git status
git log
: Displays the commit history, showing the latest commits first.
bash$ git log
git branch
: Lists all branches in the repository, with an asterisk indicating the currently active branch.
bash$ git branch
git checkout <branch-name>
: Switches to the specified branch.
bash$ git checkout development
git checkout -b <new-branch-name>
: Creates and switches to a new branch.
bash$ git checkout -b feature-branch
git pull
: Fetches and merges changes from the remote repository into the current branch.
bash$ git pull origin main
git push
: Pushes committed changes from the local repository to the remote repository.
bash$ git push origin master
git merge <branch-name>
: Merges the specified branch into the currently active branch.
bash$ git merge feature-branch
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
git remote -v
: Lists the remote repositories associated with your local repository.
bash$ git remote -v
git fetch <remote-name>
: Fetches changes from a remote repository without automatically merging them.
bash$ git fetch upstream
git diff
: Shows the differences between the working directory and the staging area.
bash$ git diff
git diff --staged
: Shows the differences between the staged changes and the last commit.
bash$ git diff --staged
git reset <file>
: Removes a file from the staging area without undoing the changes.
bash$ git reset index.html
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.