====== Git cribsheet ====== /* ~~REDIRECT>:crib:git~~ */ {{tag> crib git}} This is a [[cribsheets|personal cribsheet]] intended for my own use. If it's useful to you, great, but it doesn't cover the subject exhaustively. ===== Resources for beginners ===== * Atlassian provide some good (and Bitbucket-agnostic) learning paths: * [[https://www.atlassian.com/git/tutorials/what-is-version-control|Introduction to Git]] * [[https://www.atlassian.com/git/tutorials/setting-up-a-repository|Understanding Git commands]] * [[https://www.atlassian.com/git/tutorials/syncing|Collaborating with Git]] * [[https://www.amazon.co.uk/Rys-Git-Tutorial-Ryan-Hodson-ebook/dp/B00QFIA5OC/|Ry's Git Tutorial]] is a good, free ebook. ===== Undo ===== ==== Unstage file ==== * ''git reset %%--%% '' ==== Discard local change to file ==== * ''git checkout %%--%% '' ==== Discard all local changes ==== * ''git fetch'' * ''git checkout '' * ''git reset %%--%%hard origin/'' This is a specific form of ''git reset %%--%%hard '' which resets your current branch to ''''. ''origin/'' here refers to the HEAD commit of that branch at origin. ==== Revert ==== * ''git revert ref_spec'' Git revert undoes a **single** specified commit, making the changes necessary to do so at the HEAD of your current branch. ===== Branches ===== ==== Get existing remote branch ==== https://stackoverflow.com/questions/9537392/git-fetch-remote-branch * ''git checkout %%--%%track origin/remote_branch_name'' ==== Auto setup remote branch when pushing new local branch ==== https://medium.com/@anjusha.khandavalli/auto-setup-remote-branch-with-push-autosetupremote-892ddd7644 * ''git config --global --add --bool push.autoSetupRemote true'' ==== Branch deletion ==== === Local branches === ** ''git branch -d '' .. ''-D'' to force deletion in the presence of local-only changes === Remote branches === https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-locally-and-remotely ** ''git push -d origin '' .. (Substitute ''origin'' for a different remote name if required). === Obsolete tracking branches === https://stackoverflow.com/questions/7726949/remove-tracking-branches-no-longer-on-remote * ''git remote prune origin'' * ''git branch -vv | awk '/: gone]/{print $1}' | xargs -r git branch -d'' ===== List conflicted files ===== * ''git diff %%--%%name-only %%--%%diff-filter=U'' Show //unmerged// files in the diff between the working tree and the index (staging area for next commit). ===== Configuration ===== ==== Config files ==== git has [[https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Where-system-global-and-local-Windows-Git-config-files-are-saved|three config files]]. In Windows: * system-wide ("system") -- ''gitconfig'' in the git install folder's ''mingw64\etc'' subdirectory (probably ''C:\Program Files\Git\mingw64\etc'') * per-user ("global") -- ''.gitconfig'' in the root of the user's local profile * per-repo ("local") -- ''config'' in the repo's ''.git'' folder Settings are overridden by values in more narrowly-scoped config (e.g. if the same property is set at user and repo level, the repo level takes precedence). === Can't find them? === Inspect properties to see where their values are set: * ''git config %%--%%list %%--%%show-origin'' or just edit them (using the [[#editor|configured editor]]) via the command line: * ''git config %%--%%edit %%--%%system'' * ''git config %%--%%edit %%--%%global'' * ''git config %%--%%edit %%--%%local'' ==== Settings ==== https://www.atlassian.com/git/tutorials/setting-up-a-repository/git-config covers a lot of possible settings. === Editor === In Git for Windows, by default the mighty [[https://www.vim.org/|vim]]. If that's not your cup of tea: * ''git config %%--%%global core.editor %%"%%'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin%%"%%'' === Terminal colours === Here's a possible config setup (thanks [[https://nathanhoad.net/how-to-colours-in-git/|Nathan Hoad]]!) [color] ui = auto [color "branch"] current = yellow reverse local = yellow remote = green [color "diff"] meta = yellow bold frag = magenta bold old = red bold new = green bold [color "status"] added = green bold changed = yellow bold untracked = magenta bold To set these up via the command line (e.g. for current branch as above): * ''git config %%--%%global color.branch.current %%"%%yellow reverse%%"%%'' === Unable to get local issuer certificate === ** To disable verification: .. ''git config %%--%%global http.sslVerify false'' .. This is ill-advised as it [[https://stackoverflow.com/questions/23885449/unable-to-resolve-unable-to-get-local-issuer-certificate-using-git-on-windows/38460722|opens you up to man-in-the-middle attacks]]. === Long file names === From https://stackoverflow.com/questions/22575662/filename-too-long-in-git-for-windows. Git for Windows compiled with msys has a limit of 260 characters for a filename -- this is a limitation of msys, not Git. (Git's filename length limit is 4096 characters. Work around this by setting ''core.longpaths'' to true: ''git config %%--%%system core.longpaths true'' ===== GitHub ===== ==== Update fork from origin ==== -- Go to your forked repo -- Pull requests -> New pull request. This compares your fork to its origin as if to merge it back to its originating repo. -- Change the base repo to be your fork. This turns it into a branch comparison within the fork -- click compare across forks to be able to see the originating repo again. -- Change the head repo to the originating repo. You might need to wait for the screen to refresh if the originating repo has moved on a lot. -- Create the pull request and merge it ===== Useful links ===== * https://mirrors.edge.kernel.org/pub/software/scm/git/docs/user-manual.html#git-concepts * http://git-school.github.io/visualizing-git/