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<br/>git --version<br/>git config --global user.name "chenkang" # set user name<br/>git config --global user.email "[email protected]" # set email<br/>git config --global color.ui "true"Initialize a repository: git init Add files and commit: git add .<br/>git commit -m 'add three file' Rename a file and commit: git mv file1 file4<br/>git commit -m 'change file name' File comparison:
git diff file3 # working tree vs. staging<br/>git diff --cached file3 # staging vs. repositoryReverting changes:
# revert a staged file<br/>git reset HEAD file3<br/># revert an unstaged file<br/>git checkout -- file3Branch management:
# create a new branch<br/>git branch dev01<br/># switch to it<br/>git checkout dev01<br/># merge master into dev01<br/>git merge master -m 'merge master into dev01'<br/># switch back to master and merge dev01<br/>git checkout master<br/>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<br/>git tag v1.2<br/># delete a tag<br/>git tag -d v1.2<br/># create an annotated tag<br/>git tag -a v1.0 -m 'optimized and bug‑fixed' a119962<br/># show tag details<br/>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<br/>git archive --format tar.gz --output "./output.tar.gz" dev01<br/># archive a specific commit<br/>git archive --format tar.gz --output "./output.tar.gz" 5ca16ac0d603603<br/># archive selected directories from master<br/>git archive --format tar.gz --output "./output.tar.gz" master <name1> <name2>5. Upgrading Git on CentOS
Steps to replace the old Git version (1.8.x) with a newer one (2.23.0):
# install build dependencies<br/>yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel asciidoc -y<br/>yum install gcc perl-ExtUtils-MakeMaker -y<br/># remove old Git<br/>yum remove git -y<br/># download source tarball<br/>wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.23.0.tar.xz<br/># extract and build<br/>tar xf git-2.23.0.tar.xz<br/>cd git-2.23.0<br/>make prefix=/usr/local/git all<br/>make prefix=/usr/local/git install<br/># add new Git to PATH
echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/profileSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Practical 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.
