Git - Basic Commands

Git - Basic Commands

Basic Commands of Git

Repository Initialization

git init

The git init command initializes a new Git repository in the current directory. It creates a .git folder containing all the necessary metadata for version control.


# Initialize a new Git repository
git init

After running this command, the current directory becomes a Git-managed project.

Cloning a Repository

git clone

The git clone command is used to create a copy of an existing Git repository, usually from a remote source like GitHub or GitLab.


# Clone a remote repository
git clone https://github.com/username/repo-name.git

This command creates a new directory with the repository contents and complete commit history.

Tracking File Changes

git status

The git status command displays the current state of the working directory and staging area. It shows which files are staged, unstaged, or untracked.


# Check the status of the working directory
git status

git add

The git add command is used to stage changes for the next commit. You can add specific files or all files using a dot.


# Stage a specific file
git add index.html

# Stage all changes
git add .

git rm

The git rm command removes files from the working directory and staging area.


# Remove a file and stage the deletion
git rm old_file.txt

Committing Changes

git commit

The git commit command captures a snapshot of the staged changes. Each commit includes a message that describes the changes.


# Commit with a message
git commit -m "Add header section to homepage"

git commit --amend

You can use --amend to modify the last commit, either to change the commit message or include additional changes.


# Amend the previous commit
git commit --amend -m "Updated homepage header section"

Viewing History

git log

The git log command shows the commit history for the current branch.


# View commit history
git log

git log --oneline

To see a condensed version of the commit history:


git log --oneline

git diff

The git diff command shows the differences between files in the working directory and the staging area, or between commits.


# View changes not staged for commit
git diff

# View changes staged for the next commit
git diff --cached

Branching and Merging

git branch

The git branch command lists all branches and can also be used to create new branches.


# List all branches
git branch

# Create a new branch
git branch feature-login

git checkout

The git checkout command is used to switch branches or restore files.


# Switch to an existing branch
git checkout feature-login

git switch

A newer alternative to git checkout for switching branches:


# Switch to another branch
git switch feature-login

git merge

The git merge command combines changes from one branch into another.


# Merge feature-login into main
git checkout main
git merge feature-login

git rebase

The git rebase command is another way to integrate changes. It rewrites the commit history to create a linear sequence.


# Rebase the current branch onto main
git rebase main

Remote Repositories

git remote

The git remote command manages remote connections.


# View configured remotes
git remote -v

# Add a new remote
git remote add origin https://github.com/user/project.git

git push

The git push command sends local changes to the remote repository.


# Push changes to remote repository
git push origin main

git pull

The git pull command fetches and merges changes from a remote repository into the current branch.


# Pull changes from the remote repository
git pull origin main

git fetch

The git fetch command downloads updates from a remote repository but does not merge them.


# Fetch changes from remote
git fetch origin

Undoing Changes

git restore

The git restore command restores working directory files to their last committed state.


# Restore a modified file
git restore index.html

git reset

The git reset command un-stages files or resets commits.


# Unstage a file
git reset HEAD index.html

# Reset to a previous commit
git reset --hard 123abc

Stashing Changes

git stash

The git stash command temporarily shelves changes that are not ready to commit.


# Save local changes
git stash

# List stashes
git stash list

# Apply the most recent stash
git stash apply

# Drop the applied stash
git stash drop

Tagging

git tag

The git tag command is used to mark specific commits as versions or releases.


# Create a lightweight tag
git tag v1.0

# Create an annotated tag
git tag -a v1.1 -m "Version 1.1 release"

git show

The git show command displays information about a commit or tag.


# Show tag details
git show v1.0

Configuration

git config

The git config command sets configuration values such as username and editor.


# Set username and email
git config --global user.name "John Doe"
git config --global user.email "john@example.com"

# Set default editor
git config --global core.editor "code --wait"

# View all configuration settings
git config --list

Mastering Git’s basic commands is crucial for effective version control in any software project. These commands enable you to initialize repositories, track changes, commit updates, manage branches, interact with remotes, and undo mistakes. They form the foundation of everyday Git workflows and collaborative development.

As you become more familiar with Git, you will naturally progress to advanced commands and workflows, such as cherry-picking, bisecting, interactive rebasing, and using hooks. However, the commands covered in this document will remain essential in all stages of Git usage.

By practicing these commands regularly and understanding their purpose, you will become proficient in using Git, resulting in cleaner workflows, better collaboration, and more reliable code management.

Beginner 5 Hours
Git - Basic Commands

Basic Commands of Git

Repository Initialization

git init

The git init command initializes a new Git repository in the current directory. It creates a .git folder containing all the necessary metadata for version control.

# Initialize a new Git repository git init

After running this command, the current directory becomes a Git-managed project.

Cloning a Repository

git clone

The git clone command is used to create a copy of an existing Git repository, usually from a remote source like GitHub or GitLab.

# Clone a remote repository git clone https://github.com/username/repo-name.git

This command creates a new directory with the repository contents and complete commit history.

Tracking File Changes

git status

The git status command displays the current state of the working directory and staging area. It shows which files are staged, unstaged, or untracked.

# Check the status of the working directory git status

git add

The git add command is used to stage changes for the next commit. You can add specific files or all files using a dot.

# Stage a specific file git add index.html # Stage all changes git add .

git rm

The git rm command removes files from the working directory and staging area.

# Remove a file and stage the deletion git rm old_file.txt

Committing Changes

git commit

The git commit command captures a snapshot of the staged changes. Each commit includes a message that describes the changes.

# Commit with a message git commit -m "Add header section to homepage"

git commit --amend

You can use --amend to modify the last commit, either to change the commit message or include additional changes.

# Amend the previous commit git commit --amend -m "Updated homepage header section"

Viewing History

git log

The git log command shows the commit history for the current branch.

# View commit history git log

git log --oneline

To see a condensed version of the commit history:

git log --oneline

git diff

The git diff command shows the differences between files in the working directory and the staging area, or between commits.

# View changes not staged for commit git diff # View changes staged for the next commit git diff --cached

Branching and Merging

git branch

The git branch command lists all branches and can also be used to create new branches.

# List all branches git branch # Create a new branch git branch feature-login

git checkout

The git checkout command is used to switch branches or restore files.

# Switch to an existing branch git checkout feature-login

git switch

A newer alternative to git checkout for switching branches:

# Switch to another branch git switch feature-login

git merge

The git merge command combines changes from one branch into another.

# Merge feature-login into main git checkout main git merge feature-login

git rebase

The git rebase command is another way to integrate changes. It rewrites the commit history to create a linear sequence.

# Rebase the current branch onto main git rebase main

Remote Repositories

git remote

The git remote command manages remote connections.

# View configured remotes git remote -v # Add a new remote git remote add origin https://github.com/user/project.git

git push

The git push command sends local changes to the remote repository.

# Push changes to remote repository git push origin main

git pull

The git pull command fetches and merges changes from a remote repository into the current branch.

# Pull changes from the remote repository git pull origin main

git fetch

The git fetch command downloads updates from a remote repository but does not merge them.

# Fetch changes from remote git fetch origin

Undoing Changes

git restore

The git restore command restores working directory files to their last committed state.

# Restore a modified file git restore index.html

git reset

The git reset command un-stages files or resets commits.

# Unstage a file git reset HEAD index.html # Reset to a previous commit git reset --hard 123abc

Stashing Changes

git stash

The git stash command temporarily shelves changes that are not ready to commit.

# Save local changes git stash # List stashes git stash list # Apply the most recent stash git stash apply # Drop the applied stash git stash drop

Tagging

git tag

The git tag command is used to mark specific commits as versions or releases.

# Create a lightweight tag git tag v1.0 # Create an annotated tag git tag -a v1.1 -m "Version 1.1 release"

git show

The git show command displays information about a commit or tag.

# Show tag details git show v1.0

Configuration

git config

The git config command sets configuration values such as username and editor.

# Set username and email git config --global user.name "John Doe" git config --global user.email "john@example.com" # Set default editor git config --global core.editor "code --wait" # View all configuration settings git config --list

Mastering Git’s basic commands is crucial for effective version control in any software project. These commands enable you to initialize repositories, track changes, commit updates, manage branches, interact with remotes, and undo mistakes. They form the foundation of everyday Git workflows and collaborative development.

As you become more familiar with Git, you will naturally progress to advanced commands and workflows, such as cherry-picking, bisecting, interactive rebasing, and using hooks. However, the commands covered in this document will remain essential in all stages of Git usage.

By practicing these commands regularly and understanding their purpose, you will become proficient in using Git, resulting in cleaner workflows, better collaboration, and more reliable code management.

Related Tutorials

Frequently Asked Questions for GitHub

Teams use GitHub for version control, code sharing, pull requests, and project management.

SSH allows secure communication with GitHub for pushing and pulling code without passwords.

A release marks a specific version of code, often used for deployments or tagging milestones.

Git is a distributed version control system for tracking changes in source code efficiently.

It shows the current state of the repository, including staged, unstaged, and untracked files.


GitHub Pages hosts static websites directly from a GitHub repository.

Git is a tool; GitHub is a platform using Git for remote code collaboration.

Use git revert <commit> to undo changes by creating a new commit.

git commit saves staged changes to the local repository with a message.


Issues track bugs, tasks, or feature requests, allowing discussion and assignment.

Merging combines changes from different branches into one branch, typically main or master.


git push uploads local repository changes to a remote repository like GitHub.

GitHub Actions automates workflows like building, testing, and deploying code with CI/CD pipelines.

.gitignore specifies files and directories Git should ignore and not track.

git init initializes a new Git repository in your local project directory.

git add stages changes in files for the next commit.

A pull request proposes changes from one branch to another, usually for review and merge.

A branch allows parallel development by creating independent code versions from the main project.

GitHub is a cloud-based platform for hosting and managing Git repositories collaboratively.

The default branch name is usually main, previously known as master.

Cloning downloads a copy of a GitHub repository to your local machine.

git pull fetches and merges changes from a remote repository to your local branch.

A commit records a snapshot of file changes with a message and unique ID.

A repository stores project files, folders, and version history for collaborative development.

A fork creates a personal copy of another user's repository for independent development.


A GitHub milestone is a way to track progress on a specific goal or release by grouping related issues and pull requests.

To merge a pull request, review the proposed changes and click "Merge pull request" to integrate them into the base branch.

GitHub labels are tags that help categorize and prioritize issues and pull requests, making it easier to manage and filter them.​

To create a GitHub issue, navigate to the "Issues" tab of your repository and click "New issue."

After making changes in your forked repository, navigate to the original repository and click "New pull request" to propose your changes.

A merge conflict occurs when GitHub cannot automatically merge changes due to conflicting modifications in the same part of a file.​

To use GitHub Actions, create a YAML file in the .github/workflows directory of your repository to define your workflow.

To resolve a merge conflict, manually edit the conflicting files to combine changes, then commit the resolved files.

A branch in GitHub is a parallel version of a repository, allowing you to work on different features or fixes without affecting the main codebase.​

To add a collaborator, go to your repository's settings, select "Collaborators," and enter the GitHub username of the person you want to add.​

A GitHub Gist is a simple way to share code snippets or text, useful for sharing small pieces of code or notes.

A fork creates a personal copy of someone else's repository, allowing you to propose changes. A clone creates a local copy of a repository on your machine.​

To create a GitHub repository, log in to your GitHub account, click the "+" icon in the top right corner, and select "New repository."

To set up GitHub Pages, navigate to your repository's settings, scroll to the "GitHub Pages" section, and select the source branch.

To create a GitHub Gist, log in to your GitHub account, click the "+" icon, and select "New Gist."

A GitHub organization is a shared account where multiple people can collaborate on repositories, issues, and other GitHub features.​

The GitHub CLI is a command-line interface that allows you to interact with GitHub directly from your terminal, enabling operations like creating issues and pull requests.

o use GitHub Copilot, install the extension in a supported IDE, such as Visual Studio Code, and start typing code to receive suggestions.

To create a GitHub organization, click your profile picture in the top right corner, select "Your organizations," and click "New organization."

GitHub Copilot is an AI-powered code completion tool developed by GitHub in collaboration with OpenAI, providing suggestions as you code.​

GitHub is a web-based platform for version control and collaboration, allowing developers to host and review code, manage projects, and build software together.​

To install the GitHub CLI, download the appropriate version for your operating system from the official GitHub CLI website and follow the installation instructions.

line

Copyrights © 2024 letsupdateskills All rights reserved