[[linux_wiki:git]]

Git

General Information

Useful git commands when managing git repos.

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

Checklist

  • Distro(s): Enterprise Linux
  • Other: Git repo setup with commit access

Install

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

  • CentOS 7
    yum install https://centos7.iuscommunity.org/ius-release.rpm
  • CentOS 6
    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

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

  • Untracked ⇒ A new local file has been created and not yet added to the tracked index list
    • Proceed (add file to be tracked)
      git add <filename>
    • Undo (get rid of untracked file):
      rm <filename>


  • Unstaged changes ⇒ You made changes to an existing tracked file, but have not added the current snapshot of the file to the index yet
    • Proceed (add file to staging index)
      git add <filename>
    • Undo (undo unstaged local changes)
      git checkout <filename>


  • Uncomitted changes ⇒ You made changes to a file, and used git add to stage the change in the index, but need to commit the change to your local copy of a repo
    • Proceed (local commit changes)
      git commit <filename>
    • Undo (undo staged changes)
      git reset <filename>


  • Committed changes (clean working directory) ⇒ files are committed to the local repo and just need to be pushed to the remote repo
    • Proceed (push local committed changes to repo)
      git push
    • Undo (undo local commits)
      • Undo local commits, but preserve file contents
        git reset <last good SHA>
      • Undo local commits AND undo file content changes
        git reset --hard <last good SHA>


  • Pushed changes ⇒ You pushed changes to the repo
    • Proceed: N/A
    • Undo (Undo the last pushed change)
      • View recent commits with diff details
        git log -p
      • Retrieve the commit SHA hash from the above and revert that push
        git revert <SHA>

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/
  • Do not track any files that
    • Are inside a pycache directory
    • End in .log
    • Are inside a tmp directory


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.

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

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
  • –no-ff ⇒ retain all of the commit messages before the merge

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.

  • In the terminal, navigate to the directory of the cloned repo.
  • View current remote repos for the fork:
    git remote -v
  • Add the upstream project
    git remote add upstream https://github.com/UPSTREAM-USERNAME/APPLICATION-NAME.git
  • Verify upstream was added
    git remote -v
  • Fetch upstream's commits
    git fetch upstream
  • Check out the local master branch for your fork
    git checkout master
  • Merge changes from the upstream project into the local master branch
    git merge upstream/master
  • Push local changes to your fork on github
    git push origin master

  • linux_wiki/git.txt
  • Last modified: 2019/07/14 02:30
  • by billdozor