Comprehensive Workflow and Core Commands for Using Git
Git organizes file changes across three logical areas: a staging zone, a local repository, and a remote repository. The staging area temporarily holds modifications before they are committed, similar to an in-memory cache. The local repository stores committed snapshots persistently, analogous to disk storage. The remote repository acts as another instance of a local repository, typically hosted elsewhere for collaboration.
Configuration
Set global identity parameters used for commits:
sudo yum -y install git
git config --global user.name "devuser"
git config --global user.email "devuser@example.com"
git config --global color.ui auto
git config --list
Bare Repository Setup (For Central Sharing)
A bare repository lacks a working directory and is intended solely for receiving pushes.
sudo useradd repouser
sudo passwd repouser
mkdir /opt/repos
cd /opt/repos
git init --bare shared.git
sudo chown -R repouser:repouser shared.git
Clone the bare repository to a local workspace:
git clone repouser@192.168.67.134:/opt/repos/shared.git
Standard Local Repository Initialization
Create a regular repository to make and track changes locally:
mkdir projectX
cd projectX
git init
echo "init content" > starter.txt
git add starter.txt
git commit -m 'initial snapshot'
Stage additional files and inspect state:
echo "more data" > extra.log
git add extra.log
git status
git commit -m 'add supplemental file'
Remove a tracked file and review outcome:
rm extra.log
git status
git commit -am 'remove extra.log'
Check history and remote linkage:
git log --oneline
git remote -v
Synchronize with upstream:
git pull
Core Command Set
Initialize repository
git init
Clone existing repository
git clone <remote-url>
Stage changes
git add <pathspec>
Commit staged changes
git commit -m "descriptive message"
Inspect current state
git status
Branch operations
List branches:
git branch
Create branch:
git branch feature-alpha
Switch branch:
git checkout feature-alpha
Create and switch:
git checkout -b feature-beta
Merge branch into current:
git merge feature-alpha
Delete merged branch:
git branch -d feature-alpha
Remote interaction
Associate remote:
git remote add origin <remote-url>
Push local brench:
git push origin main
Pull updates:
git pull origin main
Show remotes:
git remote -v
Revert actions
Discard unstaged chenges in file:
git checkout -- <file>
Unstage a file while keeping changes:
git reset HEAD <file>
View history
Full log:
git log
Compact log:
git log --oneline
Adjutsing User Credentials
To view current identity settings:
git config user.name
git config user.email
Update them globally:
git config --global user.name "newname"
git config --global user.email "newemail@example.com"