Show pageOld revisionsBacklinksBack to top This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== 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 <code bash> yum install git </code> \\ ==== 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 7<code bash>yum install https://centos7.iuscommunity.org/ius-release.rpm</code> * CentOS 6<code bash>yum install https://centos6.iuscommunity.org/ius-release.rpm</code> \\ Remove the system provided version (if installed)<code bash>yum remove git</code> \\ Install the IUS git version<code bash>yum install git2u</code> \\ Verify version<code bash>git --version</code> ---- ====== Initial Git Config Setup ====== Setup your user name for commits: <code bash> git config --global user.name "USERNAME" </code> \\ 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 <code bash> git config --global user.email "YOUR-EMAIL@DOMAIN.com" </code> Option 2: Setup Git e-mail config - keep e-mail hidden <code bash> git config --global user.email "USERNAME@users.noreply.github.com" </code> \\ HTTPS Authentication setup - turn on credential cache <code bash> git config --global credential.helper cache </code> HTTPS Authentication setup - increase auth timeout to 1 hour (3600 seconds) <code bash> git config --global credential.helper 'cache --timeout=3600' </code> \\ Disable https verify for self signed certs on a single cloned repo<code bash>git config http.sslVerify false</code> \\ 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)<code bash>git init <project-name></code> \\ Download an existing project via URL<code bash>git clone URL</code> \\ Download an existing project via HTTPS URL and do not verify ssl cert for self signed<code bash>git -c http.sslVerify=false clone https://URL</code> ---- ====== File Status ====== List all new or modified files to be committed <code bash> git status </code> 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)<code bash>git add <filename></code> * Undo (get rid of untracked file):<code bash>rm <filename></code> \\ * **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)<code bash>git add <filename></code> * Undo (undo unstaged local changes)<code bash>git checkout <filename></code> \\ * **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)<code bash>git commit <filename></code> * Undo (undo staged changes)<code bash>git reset <filename></code> \\ * **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)<code bash>git push</code> * Undo (undo local commits) * Undo local commits, but preserve file contents<code bash>git reset <last good SHA></code> * Undo local commits AND undo file content changes<code bash>git reset --hard <last good SHA></code> \\ * **Pushed changes** => You pushed changes to the repo * Proceed: N/A * Undo (Undo the last pushed change) * View recent commits with diff details<code bash>git log -p</code> * Retrieve the commit SHA hash from the above and revert that push<code bash>git revert <SHA></code> ---- ====== 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<code bash># Git ignore file - comments are allowed __pycache__/ *.log tmp/</code> * 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<code bash>git ls-files --other --ignored --exclude-standard</code> ---- ====== 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 <code bash> git pull </code> \\ **=> Create new file or make existing file changes <=** \\ **Stage**: Snapshots file in preparation for versioning (stages files) <code bash> git add [file] </code> \\ **Verify**: Show file differences between staged and the last file version <code bash> git diff --staged </code> **Undo**: Unstage file, but preserve contents <code bash> git reset [file] </code> \\ **Commit Local**: Commit file snapshot to the local repo. (Either newly added file or to an existing file that was changed) <code bash> git commit [file] -m "Commit comment here." </code> \\ **Commit Remote**: Push your commits to the remote repo. <code bash> git push </code> ---- ====== Undo Changes ====== **Start Over**: You cloned a repo, made local changes that broke things. To revert back to the initially checked out version: <code bash> git checkout . git pull </code> \\ **Undo Git Add**: To revert from "git add" changes (unstage a file) <code bash> git reset </code> \\ **Undo Git Commit**: To revert from locally committed changes (git commit) <code bash> git revert </code> ---- ====== 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>') <code bash> git log </code> Details of most recent commits on a single file <code bash> git log -p <filename> </code> Details of a specific commit # <code bash> git log -p <commit#> </code> \\ ===== Diffs ===== Summary of changes that will be pushed (with next 'git push') <code bash> git diff --stat --cached origin/master </code> Details of changes that will be pushed (with next 'git push') <code bash> git diff --cached origin/master </code> ---- ====== Branches ====== View current branch <code bash> git branch </code> View all existing branches <code bash> git branch -a </code> \\ Create a new branch called "testing" <code bash> git checkout -b testing </code> Switch between 'master' and 'testing' branches <code bash> git checkout master git checkout testing </code> \\ Merge changes from 'testing' branch into the 'master' branch and push changes to the 'master' repo <code bash> git checkout master git merge testing --no-ff git push </code> * --no-ff => retain all of the commit messages before the merge ---- ===== Deleting Branches ===== Delete a remote branch<code bash>git push origin --delete <branch-name></code> \\ Delete a local branch<code bash>git branch --delete <branch-name></code> \\ Prune remote list if remote branches were deleted by someone else<code bash>git remote prune origin</code> ---- ===== 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:<code bash>git remote -v</code> * Add the upstream project<code bash>git remote add upstream https://github.com/UPSTREAM-USERNAME/APPLICATION-NAME.git</code> * Verify upstream was added<code bash>git remote -v</code> ==== Syncing Fork to Upstream ==== * Fetch upstream's commits<code bash>git fetch upstream</code> * Check out the local master branch for your fork<code bash>git checkout master</code> * Merge changes from the upstream project into the local master branch<code bash>git merge upstream/master</code> * Push local changes to your fork on github<code bash>git push origin master</code> ---- ====== Troubleshooting ====== Fixing different git issues. ===== Extra Characters in git diff ===== * Fix/remove 'ESC[xxx' characters in git diff<code bash>git config --global core.pager "less -R"</code> \\ * Fix/remove '^M' characters in git diff<code bash>git config --global core.pager "tr -d '\r' | less -REX"</code> ---- linux_wiki/git.txt Last modified: 2020/01/10 18:29by billdozor