User Tools

Site Tools


git_cheat_sheet

Git Cheat Sheet

Resources

Install

Run the following to create the user config file:

git config --global user.name "Stephen Heise"

Run the following to find where the .gitconfig file is:

git config --list --show-origin

Replace it with this:

[core]
	editor = 'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar -nosession
	pager = cat
 
[user]
	name = Stephen Heise
	email = Stephen@tallguyracing.com
 
[init]
	defaultBranch = main
 
[mergetool]
	prompt = false
	keepBackup = false
	keepTemporaries = false
 
[merge]
	tool = winmerge
 
[mergetool "winmerge"]
	name = WinMerge
	trustExitCode = true
	cmd = "/c/Program\\ Files\\ \\(x86\\)/WinMerge/WinMergeU.exe" -u -e -dl \"Local\" -dr \"Remote\" $LOCAL $REMOTE $MERGED
 
[diff]
	tool = winmerge
 
[difftool "winmerge"]
	name = WinMerge
	trustExitCode = true
	cmd = "/c/Program\\ Files\\ \\(x86\\)/WinMerge/WinMergeU.exe" -u -e $LOCAL $REMOTE
 
[push]
	default = current
	autoSetupRemote = true
 
[alias]
	st = status
	ci = commit -m
	ca = commit --amend
	br = branch
	ba = branch -a
	co = checkout
	di = diff
	dt = difftool
	fa = fetch -a
	tf = !git reset && git checkout -- . && git clean -df
	brdr = push --delete origin
	fp = push --force
	cp = cherry-pick
	ln = !git --no-pager log -n50 '--pretty=format:"%Cred%h%Creset %ad %Cgreen%<(10,trunc)%an%Creset %<(80,trunc)%s"' '--date=format:"%Y%m%d %H%M%S"'
	ro2m = !echo 'Rebasing onto master' && git fetch origin master:master && git rebase --onto master master && git pull -r origin master
	ro2mfp = !git ro2m && echo 'Force pushing' && git fp
 
[winUpdater]
	recentlySeenVersion = 2.15.1.windows.2
 
[remote "origin"]
	prune = true

Project Specific Settings

If you need project specific settings, like You Just Go, append this:

[includeIf "gitdir:C:/Dev/YouJustGo/"]
	path = .gitconfig-youjustgo

Then create a C:\Users\God\.gitconfig-youjustgo file with this:

[user]
	name = Stephen Heise
	email = stephen.heise@youjustgo.com

Verify the settings are correct by running the following. Last one wins.

git config --list --show-origin

Try Git

git init
git config --global core.editor "atom --wait" Make Atom the default editor.
git config --global user.name "Stephen Heise"
git config --global user.email "Stephen@tallguyracing.com"
git status
git add filename.txt
git commit -m "Commit message"
git log
git remote add origin https://whatever.com
git push -u origin master -u means remember these settings
git pull origin master
git diff head
git diff --staged
git reset octofamily/octodog.txt Unstages a file.
git checkout -- octocat.txt Go back to last checkout / undo.
git reset --hard origin/master Throw away all local commits.
git branch -a Show local and remote branches.
git branch clean_up Create new branch.
git checkout clean_up Switch branches.
git checkout -b new_branch Checkout and create branch at the same time.
git checkout filename.txt Undo local (unstaged) modification.
git rm '*.txt' Remove local files and include the removal in the staging area.
git rm -r folder_of_cats Recursively remove all folders and files from the given directory.
git commit -a Include the deletion of local files to staging area, do the commit.
git merge clean_up
git push origin --delete <branch_name> Delete a remote branch.
git branch -d <branch name> Delete a branch.
git branch -D <branch name> Force delete a branch. Use if branch not merged.
git remote prune origin Clean up remote branch list.
git branch -r --merged develop List remote branches that have been merged with develop.
git branch -r --no-merged develop List remote branches that have not been merged with develop.
git push
git push origin <hash>:<branch> Push up to a certain commit.
git push origin HEAD~10:<branch> Push up to the last 10 commits.
gitk A commit viewer.
gitk 'stash@{0}' View the contents of the first stash.
gitk --all $(git fsck --no-reflog | Select-String "(dangling commit )(.*)" | %{ $_.Line.Split(' ')[2] }) Find a dropped stash.
git tag -a <tagname> -m "commit comment" Annotated tag (preferred over lightweight tags).
git tag <tagname>
git push origin <tagname>
Lightweight tag (good for temporary tags).
git rebase --interactive 44447348... Fix up unpushed commit messages. '44447348…' = parent commit hash.
git log --no-merges --oneline develop.. Show all commits on the current branch.
git cherry-pick ebe6942^..905e279 Cherry-pick an inclusive commit range.

Workflows

Copy commits from one branch to another

git co <source-branch>
git rebase -i
Copy the commits you want to clipboard.
Delete all text, save, close. Aborts the rebase.
git co <target-branch>
git rebase -i Add HEAD if you only want to append commits.
Paste the commits. Delete ones you don't want.
Save, close.

Start new branch

git checkout -b branch-name Create new branch and checkout. Unstaged changes are retained.
git push --set-upstream origin branch-name Push and create branch on remote.

Get new remote branch

git pull Make sure git knows about the remote branch.
git checkout --track origin/branch_name Grab the remote branch.

Merge branch

git checkout master
git merge <branch-name>

Delete branch

git push origin --delete <branch-name> Delete the remote branch.
git branch -d <branch-name> Delete the local branch.

Rename branch

git branch -m <newname> Rename the current branch.
git branch -m <oldname> <newname> Rename any branch.
git push origin --delete <oldname>
git push origin -u <newname>
Update remote branch.

Undo last commit

git reset HEAD~ Reverts last commit, but does not change files. Only do if commit is not pushed.
git reset --hard HEAD~1 Reverts last commit and reverts changed files. Only do if commit is not pushed.

Reset tag position

git co develop Get on the correct branch.
git ls-remote --tags List remote tags.
git push --delete origin tagname Delete the remote tag.
git tag -d tagname Delete the local tag.
git tag tagname Create the local tag.
git push origin tagname Create the remote tag.
git ls-remote --tags Check you got it right.

Commit some stuff workflow

git add . Stage everything.
git stash push --keep-index Stash and keep staged changes.
git reset Unstage everything.
Hack, stage, commit…
git stash pop or git stash apply Get back original. Use apply for multi-commits. Will create merge conflicts.
VS, Take theirs Resolve conflicts.
git reset Unstage everything.

Remove last two commits from master

git co HEAD~2
git co -b masterfixed
git push --delete origin master
git br -D master
git br -m masterfixed master
git push origin --set-upstream master

Rebase onto master

git fetch origin master:master Update the master branch.
git rebase --onto master <source-branch> For example, <source-branch> = '2021.3.7946'.
git pull -r origin master
git push --force

Split up an existing commit

git rebase -i
Mark commit to be split with 'e'
Save & close
When the rebasing has stopped…
git reset HEAD~
Edit & commit.
git rebase --continue

Set up a bare repo

git init --bare BareRepo
git clone .\BareRepo\ LocalRepo URL will be something like file:///C:/Temp/BareRepo
git ci "Initial commit." --allow-empty If you need an initial commit.

Create a test repo

$scriptpath = Split-Path $MyInvocation.MyCommand.Path
Push-Location $scriptpath
 
$remoteRepoName = 'RemoteRepo'
$localRepoName = 'LocalRepo'
 
mkdir $remoteRepoName
git init --bare $remoteRepoName
git clone $remoteRepoName $localRepoName
 
# Create an initial commit - optional
git --git-dir "$localRepoName/.git" commit --allow-empty -m 'Initial commit.'
git --git-dir "$localRepoName/.git" push
 
Pop-Location

Import from SVN

git svn clone https://ares/svn/SVNRepository/Presentations --no-metadata --tags=Tags --trunk=Trunk --branches=Branches --authors-file=..\users.txt
git_cheat_sheet.txt · Last modified: 2022/07/22 03:17 by stephen