General Information
Useful git commands when managing git repos.
Official site: https://git-scm.com/
Checklist
Git can be installed from repos (older, stable versions) or from source (newest available).
Git is available via CentOS default repos
yum install git
IUS (Inline with Upstream Stable) is a Rackspace sponsored community project to build more recent versions of RPMs for Enterprise Linux.
It is similar to Software Collections, but is a bit easier to use since there is not an environment to “enable”.
Add the IUS Repo
yum install https://centos7.iuscommunity.org/ius-release.rpm
yum install https://centos6.iuscommunity.org/ius-release.rpm
Remove the system provided version (if installed)
yum remove git
Install the IUS git version
yum install git2u
Verify version
git --version
Setup your user name for commits:
git config --global user.name "USERNAME"
E-mail config can be setup with your real e-mail or by keeping your e-mail private:
Option 1: Setup Git e-mail config - real e-mail
git config --global user.email "YOUR-EMAIL@DOMAIN.com"
Option 2: Setup Git e-mail config - keep e-mail hidden
git config --global user.email "USERNAME@users.noreply.github.com"
HTTPS Authentication setup - turn on credential cache
git config --global credential.helper cache
HTTPS Authentication setup - increase auth timeout to 1 hour (3600 seconds)
git config --global credential.helper 'cache --timeout=3600'
Disable https verify for self signed certs on a single cloned repo
git config http.sslVerify false
Alternatively, configure git to trust self signed certs: https://stackoverflow.com/questions/9072376/configure-git-to-accept-a-particular-self-signed-server-certificate-for-a-partic
Create a new local project (to be pushed up to a repo later)
git init <project-name>
Download an existing project via URL
git clone URL
Download an existing project via HTTPS URL and do not verify ssl cert for self signed
git -c http.sslVerify=false clone https://URL
List all new or modified files to be committed
git status
File States
git add <filename>
rm <filename>
git add <filename>
git checkout <filename>
git commit <filename>
git reset <filename>
git push
git reset <last good SHA>
git reset --hard <last good SHA>
git log -p
git revert <SHA>
A plain text file called '.gitignore' in the project root excludes files and directories from tracking by git.
Full documentation here: https://git-scm.com/docs/gitignore
Example .gitignore
# Git ignore file - comments are allowed __pycache__/ *.log tmp/
List all ignored files in the current project
git ls-files --other --ignored --exclude-standard
The general workflow of making file changes to a git repo:
Pull: Update local repo to include remote repo's latest changes
git pull
⇒ Create new file or make existing file changes ⇐
Stage: Snapshots file in preparation for versioning (stages files)
git add [file]
Verify: Show file differences between staged and the last file version
git diff --staged
Undo: Unstage file, but preserve contents
git reset [file]
Commit Local: Commit file snapshot to the local repo. (Either newly added file or to an existing file that was changed)
git commit [file] -m "Commit comment here."
Commit Remote: Push your commits to the remote repo.
git push
Start Over: You cloned a repo, made local changes that broke things. To revert back to the initially checked out version:
git checkout . git pull
Undo Git Add: To revert from “git add” changes (unstage a file)
git reset
Undo Git Commit: To revert from locally committed changes (git commit)
git revert
You can see file differences and logging of changes at different stages in the process.
Summary of recent commits (by 'git commit <filename>')
git log
Details of most recent commits on a single file
git log -p <filename>
Details of a specific commit #
git log -p <commit#>
Summary of changes that will be pushed (with next 'git push')
git diff --stat --cached origin/master
Details of changes that will be pushed (with next 'git push')
git diff --cached origin/master
View current branch
git branch
View all existing branches
git branch -a
Create a new branch called “testing”
git checkout -b testing
Switch between 'master' and 'testing' branches
git checkout master git checkout testing
Merge changes from 'testing' branch into the 'master' branch and push changes to the 'master' repo
git checkout master git merge testing --no-ff git push
Delete a remote branch
git push origin --delete <branch-name>
Delete a local branch
git branch --delete <branch-name>
Prune remote list if remote branches were deleted by someone else
git remote prune origin
If your cloned repo is a fork, you will want to keep it synced with the upstream project from time to time.
Adding the upstream repo to your local cloned fork only needs to be done one time.
git remote -v
git remote add upstream https://github.com/UPSTREAM-USERNAME/APPLICATION-NAME.git
git remote -v
git fetch upstream
git checkout master
git merge upstream/master
git push origin master
Fixing different git issues.
* Fix/remove 'ESC[xxx' characters in git diff
git config --global core.pager "less -R"
* Fix/remove '^M' characters in git diff
git config --global core.pager "tr -d '\r' | less -REX"