====== 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). ===== Repos ===== Git is available via CentOS default repos yum install git \\ ==== IUS Repos ==== [[https://ius.io/|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 7yum install https://centos7.iuscommunity.org/ius-release.rpm * CentOS 6yum install https://centos6.iuscommunity.org/ius-release.rpm \\ Remove the system provided version (if installed)yum remove git \\ Install the IUS git versionyum install git2u \\ Verify versiongit --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 repogit 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 \\ Download an existing project via URLgit clone URL \\ Download an existing project via HTTPS URL and do not verify ssl cert for self signedgit -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 * Undo (get rid of untracked file):rm \\ * **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 * Undo (undo unstaged local changes)git checkout \\ * **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 * Undo (undo staged changes)git reset \\ * **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 contentsgit reset * Undo local commits AND undo file content changesgit reset --hard \\ * **Pushed changes** => You pushed changes to the repo * Proceed: N/A * Undo (Undo the last pushed change) * View recent commits with diff detailsgit log -p * Retrieve the commit SHA hash from the above and revert that pushgit revert ---- ====== 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 projectgit 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 ') git log Details of most recent commits on a single file git log -p Details of a specific commit # git log -p \\ ===== 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 * --no-ff => retain all of the commit messages before the merge ---- ===== Deleting Branches ===== Delete a remote branchgit push origin --delete \\ Delete a local branchgit branch --delete \\ Prune remote list if remote branches were deleted by someone elsegit 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. * In the terminal, navigate to the directory of the cloned repo. * View current remote repos for the fork:git remote -v * Add the upstream projectgit remote add upstream https://github.com/UPSTREAM-USERNAME/APPLICATION-NAME.git * Verify upstream was addedgit remote -v ==== Syncing Fork to Upstream ==== * Fetch upstream's commitsgit fetch upstream * Check out the local master branch for your forkgit checkout master * Merge changes from the upstream project into the local master branchgit merge upstream/master * Push local changes to your fork on githubgit push origin master ---- ====== Troubleshooting ====== Fixing different git issues. ===== Extra Characters in git diff ===== * Fix/remove 'ESC[xxx' characters in git diffgit config --global core.pager "less -R" \\ * Fix/remove '^M' characters in git diffgit config --global core.pager "tr -d '\r' | less -REX" ----