Files
2026-01-19 14:21:44 +03:30

5.2 KiB

Git Commands Guide (DevOps-Oriented)

1. Installation and Setup

Install Git

Download and install Git from: https://git-scm.com/

Linux (Debian/Ubuntu):

sudo apt update && sudo apt install git -y

RHEL/CentOS:

sudo yum install git -y

macOS (Homebrew):

brew install git

Verify Installation

git --version

Configure User Identity

Git uses this information for commits:

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

Check configuration:

git config --list

Configuration scopes:

  • --system: All users
  • --global: Current user
  • --local: Repository only

2. SSH Key Configuration

Generate SSH Key

ssh-keygen -t ed25519 -C "your.email@example.com"

Start SSH agent and add key:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Use Custom SSH Key (Per Repository)

git config --local core.sshCommand "ssh -i <PATH_TO_SSH_KEY>"

Clone with custom SSH key:

git -c core.sshCommand="ssh -i <key-path>" clone git@host:repo.git

3. Initialize Repository

Create a new Git repository:

git init -b main

Existing repository:

git init

4. Basic Workflow

Stage and Commit Changes

Stage all changes:

git add -A

Commit changes:

git commit -m "Initial commit"

Connect Local Repository to Remote

git remote add origin <REPO_URL>

Verify:

git remote -v

Push to Remote

First push:

git push -u origin main

Subsequent pushes:

git push

5. Repository Status and History

Check Repository Status

git status

View Commit History

git log

Common options:

git log --oneline
git log --graph --oneline --all
git log -p
git log -3

View File Changes

Unstaged changes:

git diff

Staged changes:

git diff --staged

Compare branches:

git diff main..dev

6. File Operations

Stage Specific Files

git add <file>

Unstage Files

git reset <file>

Discard Local Changes

git checkout -- <file>

Restore using modern command:

git restore <file>

Rename File

git mv old-name new-name

Remove File

git rm <file>

Remove but keep locally:

git rm --cached <file>

7. Branch Management

Create and Switch Branch

git checkout -b <branch-name>

Modern alternative:

git switch -c <branch-name>

List Branches

git branch
git branch -a
git branch -v

Delete Branch

git branch -d <branch-name>

Force delete:

git branch -D <branch-name>

Rename Branch

git branch -m old-name new-name

8. Merging and Rebasing

Merge Branch

git merge <branch-name>

Merge types:

  • Fast-forward
  • Three-way merge (creates merge commit)

Rebase (Linear History)

git rebase main

Abort rebase:

git rebase --abort

Continue rebase:

git rebase --continue

9. Remote Operations

List Remotes

git remote
git remote -v

Show Remote Details

git remote show origin

Fetch Changes

git fetch
git fetch --all

Pull Changes

Fetch + merge:

git pull

Rebase instead of merge:

git pull --rebase

10. Commit Management

Amend Last Commit

git commit --amend

Show Commit Details

git show <commit-id>

Revert Commit (Safe for Shared Branches)

git revert <commit-id>

Reset Commit (Use with Caution)

Soft reset:

git reset --soft HEAD~1

Mixed reset:

git reset HEAD~1

Hard reset:

git reset --hard HEAD~1

11. Stash (Temporary Changes)

Save work without committing:

git stash

List stashes:

git stash list

Apply stash:

git stash apply

Pop stash:

git stash pop

12. Tags (Releases)

Create tag:

git tag v1.0.0

Annotated tag:

git tag -a v1.0.0 -m "Release v1.0.0"

Push tags:

git push origin --tags

13. .gitignore

Create .gitignore:

touch .gitignore

Example:

.env
node_modules/
*.log

Apply after commit:

git rm -r --cached .
git add .
git commit -m "Apply gitignore"

14. Useful Configuration and Aliases

Change default editor:

git config --global core.editor "vim"

Create aliases:

git config --global alias.st status
git config --global alias.co checkout
git config --global alias.cm commit
git config --global alias.br branch

15. Troubleshooting and Recovery

Undo last commit but keep changes:

git reset --soft HEAD~1

Recover deleted branch:

git reflog
git checkout -b <branch-name> <commit-id>

Fix detached HEAD:

git checkout main

16. Clone Repository

Clone via SSH:

git clone git@github.com:user/repo.git

Clone specific branch:

git clone -b <branch> <repo-url>