0% found this document useful (0 votes)
17 views

Git Operations

Uploaded by

lsambhu005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Git Operations

Uploaded by

lsambhu005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Here’s an overview of commonly used Git operations in Linux, along with explanations and

syntax.

1. Git Initialization and Configuration

Initialize a new repository

Command: git init

Explanation: Creates a new Git repository in the current directory. This is done only once for
each project.

Syntax:

git init

Configure user information (username and email)

Command:

git config --global user.name "Your Name"


git config --global user.email "[email protected]"

Explanation: Sets your username and email for Git commits globally (for all repositories on your
system).

2. Adding Files

Add files to staging area

Command: git add

Explanation: Moves changes from the working directory to the staging area. You can specify
individual files or use . to add all changes.

Syntax:

git add <filename> # Adds specific file


git add . # Adds all files in current directory
3. Committing Changes

Commit changes in the staging area

Command: git commit

Explanation: Saves the staged changes to the repository history with a message describing the
changes.

Syntax:

git commit -m "Your commit message here"

4. Viewing History and Status

Check status of files

Command: git status

Explanation: Shows the status of changes, indicating staged, unstaged, and untracked files.

Syntax:

git status

View commit history

Command: git log

Explanation: Displays a log of all commits in the repository’s history.

Syntax:

git log

5. Branching and Merging

Create a new branch


Command: git branch

Explanation: Creates a separate branch to work on features independently from the main
branch.

Syntax:

git branch <branch_name>

Switch to another branch

Command: git checkout

Explanation: Moves to the specified branch.

Syntax:

git checkout <branch_name>

Merge branches

Command: git merge

Explanation: Integrates changes from one branch into another (usually merges a feature branch
into the main branch).

Syntax:

git checkout main # Switch to the branch where you want to merge
git merge <branch_name> # Merge the specified branch into the current branch

6. Remote Repositories

Add a remote repository

Command: git remote add

Explanation: Connects your local repository to a remote server like GitHub or GitLab.
Syntax:

git remote add origin <repository_URL>

Push changes to remote

Command: git push

Explanation: Uploads local commits to a remote repository.

Syntax:

git push origin <branch_name>

Pull changes from remote

Command: git pull

Explanation: Fetches changes from a remote repository and merges them into the current
branch.

Syntax:

git pull origin <branch_name>

7. Cloning a Repository

Clone an existing repository

Command: git clone

Explanation: Copies a repository from a remote location to your local machine.

Syntax:

git clone <repository_URL>

8. Discarding Changes
Discard changes in a file

Command: git checkout -- <filename>

Explanation: Reverts changes in a specific file back to the last committed state.

Syntax:

git checkout -- <filename>

Unstage changes

Command: git reset

Explanation: Removes files from the staging area but keeps changes in the working directory.

Syntax:

git reset <filename>

9. Reverting Commits

Undo a commit

Command: git revert

Explanation: Creates a new commit that reverses the changes made by a specific commit,
preserving history.

Syntax:

git revert <commit_hash>

Each of these commands can help you efficiently manage your Git repositories in a Linux
environment.

You might also like