Git Commands

This guide helps you get fimalier with the basic git commands and understand the commands definition.

1. Getting Started

Git init

This command is used to initialize a new git repository or to add an existing repository to a new directory. Transform the current directory into a Git repository. This adds a .git subdirectory to the current directory and makes it possible to start recording revisions of the project.

# It creates a new .git subdirectory in the current working directory
git init 

Git config

A convenient way to set configuration options for your Git installation. You’ll typically only need to use this immediately after installing Git on a new development machine.

git config --global user.name "your-name"  
git config --global user.email "your_email@example.com"  

Git clone

Creates a copy of an existing Git repository. Cloning is the most common way for developers to obtain a working copy of a central repository.

# Copies an existing git repository to a new location
git clone [repository-url]   

2. Making Changes

Git add

Moves changes from the working directory to the staging area. This gives you the opportunity to prepare a snapshot before committing it to the official history.

# Adds all changes in the current directory and subdirectories
git add .   
# Adds only the filename.md in the current directory and subdirectories
git add [filename.md]   

Git commit

Takes the staged snapshot and commits it to the project history. Combined with git add, this defines the basic workflow for all Git users.

# Commits the changes in the current directory and subdirectories
git commit -m "type(scope): your message"   

Please follow the contributing guidelines for the best practices.

3. Working with Branches

Git branch

This command is your general-purpose branch administration tool. It lets you create isolated development environments within a single repository.

# Creates a new branch
git branch [branch-name]  

Git checkout

Switches between branches. It updates the working directory to match the branch you want to switch to.

# Switches to the branch
git checkout [branch-name] 

4. Sharing Changes

Git push

Pushes the changes to the remote repository.

# Pushes the changes to the remote repository
git push origin [branch-name]   

Git pull

Pulls the changes from the remote repository. Pulling is the automated version of git fetch. It downloads a branch from a remote repository, then immediately merges it into the current branch.

# Pulls the changes from the remote repository
git pull origin [branch-name]   

Git fetch

Fetches the latest data from a remote repository. It downloads the data from the remote repository, but does not merge it into your local branch.

# Fetches the latest data from the remote repository
git fetch origin [branch-name]   

5. Undoing Changes

Git rebase

Rebases the current branch onto the specified branch. It replays your commits on top of the specified branch.

# Rebases the current branch onto the specified branch
git rebase [branch-name]  
# Rebases the current branch onto the specified branch and allows you to edit the commit history 
git rebase -i [branch-name]  
# Aborts the rebase operation  
git rebase --abort  
# Continues the rebase operation
git rebase --continue   
# Skips the current commit
git rebase --skip    

Git reset

Resets the current branch to the specified commit. It discards all changes since the specified commit.

# Resets the current branch to the specified commit
git reset [commit-hash]   

Git revert

Reverts the specified commit. It restores the working directory to the state it was in before the commit was made.

# Reverts the specified commit
git revert [commit-hash] 

6. Viewing History

Git log

Displays the commit history.

# Displays the commit history
git log   

Git status

Displays the status of the repository.

# Displays the status of the repository
git status