GIT - auto-mate/CheatSheetWiki GitHub Wiki

GIT

status

show any required add or commit details on recovering pre changed file

git status 

start new repository in directory

in selected directory run

git init

add files pre commit

from git directory

git add .

for ALL files or

git add <filename>   

for single file

commit

e.g. an original commit

git commit  -m "Original_Commit_or_Original_HEAD"

show a commit file in windows CMD

from git directory

git show HEAD~0:"Filename"  

NB change from ~0 to ~1 for one version back etc... NB in "Bash" shows file contents in CMD shows comments and info

show changed files

git add -n .

then add if required

git add  .

and commit if required

git commit  -m "Second_Commit_New_HEAD"  

restore earlier committed file in windows CMD

git checkout Head~0 -- "Test Get"

NB change from ~0 to ~1 for one version back etc...

show diff

git diff --name-only Head~2 Head~3

Or

git diff  Head~2 Head~3

for detail

shared network walkthrough

git Additional Notes...

{ IN DIR FOR MASTER SOURCE (dist)
{ git init --bare 

{ IN INITIAL DEV USERS AREA 
{ git init
{ NB add a .gitignore as the first file and commit it before other files and commits
{ ADD Files and edit then
{ git add .
{ git commit -m "your commit message"
{ git remote add "TestRepoOrWhatever" "C:\path\to\master\dist"
{ git push --set-upstream "TestRepoOrWhatever" main
{ git pull dist main

NB main used to be master

{ to get data from "C:\path\to\master\dist" into LIVE or TEST
{ git init
{ git pull "C:\path\to\master\dist"

push to bare repo initially

git SET UP

1. Make 3 Folders Suggested Names  
 a, Creator  
 b, Archive  
 c, Helper  

2. in cmd/bash cd into folder Creator run "git init", same in folder Helper  
3. in cmd/bash cd into folder Archive run "git init --bare" 

ADD FILES TO CREATOR FOLDER and record in GIT

4. in folder Creator make something e.g. new text file  
5. in cmd/bash cd into folder Creator run "git add ."  
6. in cmd/bash cd into folder Creator run "git commit -m "some message text" "  

TELL GIT IN CREATOR FOLDER TO USE ARCHIVE FOLDER AS A REMOTE MASTER

7. in cmd/bash folder Creator "git remote add "NameForArchive" pathTo/Archive"  
8. "git push --set-upstream OurArchive master"  

LOAD CURRENT DATA FROM ARCHIVE TO OUR HELPER LOCATION

9. in cmd/bash folder Helper "git pull pathTo/Archive"   (run git init first)

HELPER EDITS FILE

10.Write something in pulled in text file in HELPER folder  

CREATOR ADDS NEW FILE

11.Add new file in CREATOR folder.  

HELPER and CREATOR Commit

12. in cmd/bash cd into folder Creator run "git add ." (same in folder HELPER)  
13. in cmd/bash cd into folder Creator run "git commit -m "some message text" " (same in folder HELPER)  

TRY PUSH FROM HELPER

14. Ensure you run "git remote add "NameForArchive" pathTo/Archive" in folder HELPER  
15. Run "git push --set-upstream OurArchive master"  

TRY PUSH FROM CREATOR FOLDER

16. "git push" [rejects as out of date]  
17. try "git pull" [goes into vim] type ": for command f1 help :w to save :q to quit"  
18. "git push" now updates into OurArchive  

HELPER OUT OF DATE - FIX

19. try "git pull"  

CHECK BRANCH IN HELPER

20. type "git branch"   

NEW BRANCH IN HELPER

21. type "git branch "dev"" makes a new dev branch  

SWITCH BRANCH IN HELPER

21. type "git checkout "dev"" changes to dev branch  

UPDATE BRANCH IN HELPER

22. make a new file and type "git add ." followed by "git commit -m "updadeDevMessage""  

SEND BRANCH TO ARCHIVE

23. "git push -u OurArchive "dev".  Pushes the branch NB -u IS THE SAME AS --set-upstream  

GET BRANCH DEV FROM ARCHIVE [remote name OurArchive]

24 In CREATOR run "git pull OurArchive dev" [should work with just git pull but might not be visible in git-branch??]  

make actual files (working copy) reflect a branch

use "git branch" to show branches  
use "git checkout branchName" to make actual files reflect repository content for the selected branch  

roll back and return

to roll back to an earlier commit

git log

choose sha of required version

git checkout shaNumber[enough of to id]

to return e.g. in master

git checkout master  

merge

to merge fix into master ensure you are in master

git checkout master

now merge fix

git merge fix

Simple Restore

To go back to last commit

git checkout -- .

In correct directory.... list commits to choose from

git log --oneline   

choose to change files in directory to go back 2 versions

git checkout head~2  

choose to load "file n" to working from 2 commits back

git checkout head~2  "file n"  

reset to working files to master

git checkout master

View Remote

-v for verbose

git remote -v

gitignore

add a .gitignore file before any other and commit withe content you dont want. Once a file is in the repo it will be tracked whatever you put in .gitignore !

Name And Email

git config --global user.email "<SOME_FIRST_NAME.SOME_SECOND_NAME>"  
git config --global user.email "<[email protected]>"  

Excel

Sub git(msg)

    ' * Save new spreadsheet in a DevFolder with Subfolder GIT
    ' * Save this in your spreadsheet in a Module called git
    ' * Make sure your DevFolder has had git -init run on it at the command line
    ' * Work On your Module
    ' * When you want to commit
    '    * Be in the module you want to commit
    '    * type git.git "<some message>" in the immediate pane

    CurrentPage = ThisWorkbook.VBProject.VBComponents.VBE.ActiveCodePane.CodeModule.Lines(1, ThisWorkbook.VBProject.VBComponents.VBE.ActiveCodePane.CodeModule.CountOfLines)
    CurrentModule = ThisWorkbook.VBProject.VBComponents.VBE.ActiveCodePane.CodeModule.Parent.Name
    currentFile = ThisWorkbook.Name
    gitName = currentFile & "_" & CurrentModule & ".txt"

    If Dir(gitName) <> "" Then
        FileSystem.Kill gitName
    End If


    Set fso = CreateObject("Scripting.FileSystemObject")
    Set OutFolder = fso.getfolder(ThisWorkbook.Path & "\GIT")
        OutFolder.CreateTextFile (gitName)


    Set fsoOut = fso.openTextFile("GIT\" & gitName, 8)

    fsoOut.write CurrentPage
    fsoOut.Close

    Set fsoOut = Nothing
    Set OutFile = Nothing
    Set OutFolder = Nothing
Set fso = Nothing

    Set sh = CreateObject("WScript.Shell")
        sh.CurrentDirectory = ThisWorkbook.Path
        Set shRtn = sh.exec("git commit -a -m  """ & msg & """")
        Select Case shRtn.Status
            Case WshFinished
                strOutput = shRtn.StdOut.ReadAll
            Case WshFailed
                strOutput = shRtn.StdErr.ReadAll
        End Select
        Debug.Print strOutput
    Set sh = Nothing
End Sub

git init

Run "git init --bare" in the directory where creating master repo to push pull from.

Run "git init" in the directory where there are files to work on or use, then use as standalone or clone from the --bare repo set up as the archive repo.

create a .gitignore in the dev location from the start, once committed a file stays that way.

add files with * or filename

git add *

update files with

git commit -a -m "<YourMessage>"   

refresh with

git pull  

send to remote with

git push  

in production bring up to date with

:: run git log to show last commit - copy hash  
git log  
git pull  
:: if there is a problem reset with hash from above  
git checkout hash  

reset to match remote

Delete your folder and git clone again from its parent folder

OR

If required save your current work first

git commit -a -m "Pre Reset Work"  
git branch Pre-Reset-Work  

git fetch <remote-name-e.g.-origin>  
git reset --hard <remote-name-e.g.-origin>/<remote-branch-e.g./master>  

roll back git database

In Dev

roll back git database

:: to go back one revision
git reset HEAD~1  

:: if you want to reset the working copy back to the last commit run 

git checkout .  

:: remove any untracked items with (remove --dry-run if happy)  
git clean -f  --dry-run
git clean -fd --dry-run 

In VSC

For an (On Site Archive/Dev/Production Model)

Set Up
User

  • git config --global user.email "<SOME_FIRST_NAME.SOME_SECOND_NAME>"
  • git config --global user.email "<[email protected]>"

In Archive Folder

  • git init --bare

In Dev Folder

  • git init;
  • git clone ;
    NB make a .gitignore file;

Working in dev

  • git add * or .
  • git commit -a -m "<YourMessage>"
  • git status (confirm local database status is ready)
  • git pull (to ensure using up to date repo)
  • git push

Working on a new branch
From master - Make New Branch

  • git branch <new branch name>

To work in branch

  • git switch <new branch name>

Commit changes In Branch

  • git commit -a -m "your message"

To Merge changes into master

  • git switch master
  • git merge

Send to archive

  • git push

Git merge conflicts in VSC

In VSC with GitLens use Accept Button to choose from options in conflict files
Save the amended file (you will still be in master)
Run Stage Changes option (you will still be in master)
At Command line (you will still be in master) Run

  • git commit

Edit Commit Info File it will display if Required and Close

Working in Prod
Initial Setup

  • git init;
  • git clone <path>;

Making Changes

  • git log (see where we are);
  • git pull;

For any problem to reset immediately use

  • git checkout <last good hash from git log above> *

A Little Extra

Problems in Dev
Undo commit (keep running to step back)
Best way (leaves record)

  • git revert
    OR
  • git reset HEAD~1 (OR git revert head to keep a record)

Reset working copy with

  • Git checkout .

Merger errors

  • git reset --merge

Change Message of last commit

  • git commit --amend -m "new message"

Branches
List

  • git branch

Make

  • git branch "dev"

Switch

  • git switch master

Merge

  • git switch master;
  • git merge <branchname>

Undo Merge (git merge --abort) or

  • git reset --hard <shacode> (dangerous - get sha from git log before merge)

Finding Errors
Changes between any 2 shas

  • git diff <older_sha1> <newer_sha2> <blank or list of files>

Set Files to a sha but keep all commits in database view
Use * or list of file(s)

  • git checkout <sha_you_want_to_go_back_to> *

Set files and database all back to master

  • git checkout master *
    (if any problems use git switch master and retry)

show old version of a file as command line output

Requires / in windows too

git show <PartOfSha>:./filename  

NB without ":"

git show <PartOfSha> ./filename  

will show changes

Working With Bare Repos

Show SHAs In last 2 weeks

git log master --since="2 weeks ago"  

Show Changes in a SHA

git show <sha or part of>  

Show Changes latest changes

git show master  

Show File As Is Currently

git show master:Help/AHelpFile.html  

Show File As At Sha

git show <full sha>:Help/AHelpFile.html  

git_test_debug_unsafe_directories error (in windows add "*" for all or the dir for just that directory use /)

git config --global --add safe.directory "*"  

Show Initial Commit Abbreviated Ref use %H for Full ref

git log --reverse -1 --pretty="%h"  

Files in a commit

git show --name-only --pretty=""  

If "Less" truncates lines use

git --no-pager show 7ce4205:booked.php | more  

or

git --no-pager show 7ce4205:booked.php  

Git Config and Dubious Ownership

fatal: detected dubious ownership in repository at ....
look for ".config" in repo folder,
and ".gitconfig" in %HOMESHARE%, %HOMEDRIVE%, %HOMEPATH%, %userprofile%
add this to the file.

[safe]
    directory = * 
⚠️ **GitHub.com Fallback** ⚠️