Table of Contents

Git

General Information

Useful git commands when managing git repos.

Official site: https://git-scm.com/

Checklist


Install

Git can be installed from repos (older, stable versions) or from source (newest available).

Repos

Git is available via CentOS default repos

yum install git


IUS Repos

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


Remove the system provided version (if installed)

yum remove git


Install the IUS git version

yum install git2u


Verify version

git --version

Initial Git Config Setup

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/Clone Repos

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

File Status

List all new or modified files to be committed

git status

File States






Exclude Files from Tracking

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

Pull, Change, Push

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

Undo Changes

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

Logs and Diffs

You can see file differences and logging of changes at different stages in the process.

Logs

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#>


Diffs

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

Branches

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

Deleting Branches

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

Upstream Repo Sync

If your cloned repo is a fork, you will want to keep it synced with the upstream project from time to time.

Add Upstream Source

Adding the upstream repo to your local cloned fork only needs to be done one time.

Syncing Fork to Upstream


Troubleshooting

Fixing different git issues.

Extra Characters in git diff

* 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"