Git Version Control: Installation, Configuration, Core Commands, Branch & Tag Management, and Code Export
This guide walks through installing and configuring Git, explains the working directory, staging area, and repository concepts, demonstrates common commands for initializing, committing, branching, tagging, reverting changes, and shows how to archive code and upgrade Git on CentOS.
1. Install Git Version Control System
Git's working directory, staging area, and repository meanings
Working directory: the visible folder on your computer.
Staging area: also called stage or index, stored in .git/index .
Repository: the hidden .git folder that holds all history.
Installation and configuration commands:
yum install git -y
git --version
git config --global user.name "chenkang" # set user name
git config --global user.email "[email protected]" # set email
git config --global color.ui "true"Initialize a repository:
git initAdd files and commit:
git add .
git commit -m 'add three file'Rename a file and commit:
git mv file1 file4
git commit -m 'change file name'File comparison:
git diff file3 # working tree vs. staging
git diff --cached file3 # staging vs. repositoryReverting changes:
# revert a staged file
git reset HEAD file3
# revert an unstaged file
git checkout -- file3Branch management:
# create a new branch
git branch dev01
# switch to it
git checkout dev01
# merge master into dev01
git merge master -m 'merge master into dev01'
# switch back to master and merge dev01
git checkout master
git merge dev01 -m 'merge dev01 into master'Branching principles:
master should be stable and used only for releases.
development occurs on dev (or feature) branches.
individual developers work on personal branches and merge into dev.
use --no-ff for a true merge commit to preserve history.
3. Git Tag Management
Tags give commits a readable alias.
# create a lightweight tag
git tag v1.2
# delete a tag
git tag -d v1.2
# create an annotated tag
git tag -a v1.0 -m 'optimized and bug‑fixed' a119962
# show tag details
git show v1.14. Exporting Code with Git Archive
Archive a branch:
git archive --format tar.gz --output "./output.tar.gz" masterArchive a specific commit or directory:
# archive a different branch
git archive --format tar.gz --output "./output.tar.gz" dev01
# archive a specific commit
git archive --format tar.gz --output "./output.tar.gz" 5ca16ac0d603603
# archive selected directories from master
git archive --format tar.gz --output "./output.tar.gz" master5. Upgrading Git on CentOS
Steps to replace the old Git version (1.8.x) with a newer one (2.23.0):
# install build dependencies
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel asciidoc -y
yum install gcc perl-ExtUtils-MakeMaker -y
# remove old Git
yum remove git -y
# download source tarball
wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.23.0.tar.xz
# extract and build
tar xf git-2.23.0.tar.xz
cd git-2.23.0
make prefix=/usr/local/git all
make prefix=/usr/local/git install
# add new Git to PATH
echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/profilePractical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.