git: notes and tips This note was created on 2024-05-07 This note was last edited on 2024-05-07 === Setup === Set a name that is identifiable for credit when review version history: $ git config --global user.name “[firstname lastname]” Note: remove "--global" flag to set settings only for current repo. Set an email address that will be associated with each history marker: $ git config --global user.email “[valid-email]” Note: remove "--global" flag to set settings only for current repo. Set automatic command line coloring for Git for easy reviewing: $ git config --global color.ui auto Save HTTP credentials (stored as plain text file on disk): $ git config --global credential.helper store Save credantils in macOS keychain: $ git config --global credential.helper=osxkeychain Use configuration file "~/.gitconfig" for configuration: ~~~ [user] name = FirstName LastName email = email@example.com signingkey = XXXXXXXXXXXXXXXXXXXXXXXXX [gpg] program = gpg [commit] gpgsign = true ~~~ === Init === Initialize current directory as a Git repository: $ git init Create new directory and nitialize as a Git repository: $ git init ./repo-dir-name Retrieve an entire repository from a hosted location via URL: $ git clone [url] === Stage and snapshot === Show modified files in working directory, staged for your next commit: $ git status Add a file as it looks now to your next commit (stage): $ git add [file] Unstage a file while retaining the changes in working directory: $ git reset [file] Diff of what is changed but not staged: $ git diff Diff of what is staged but not yet committed: $ git diff --staged Commit your staged content as a new commit snapshot: $ git commit -m “[descriptive message]” === Branch and manage === List your branches. a * will appear next to the currently active branch: $ git branch Create a new branch at the current commit: $ git branch [branch-name] Switch to another branch and check it out into your working directory: $ git checkout Merge the specified branch’s history into the current one: $ git merge [branch] === Inspect and compare === Show all commits in the current branch’s history: $ git log Show the commits on branchA that are not on branchB: $ git log branchB..branchA Show the commits that changed file, even across renames: $ git log --follow [file] Show the diff of what is in branchA that is not in branchB: $ git diff branchB...branchA Show any object in Git in human-readable format: $ git show [SHA] View commit tree: $ git log --all --graph --decorate --oneline $ git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%C(bold blue) <%an> %Creset' --abbrev-commit === Removing files and changing paths === Delete the file from project and stage the removal for commit: $ git rm [file] Change an existing file path and stage the move: $ git mv [existing-path] [new-path] Show all commit logs with indication of any paths that moved: $ git log --stat -M === Share and update === Add a git URL as an alias: $ git remote add [alias] [url] Fetch down all the branches from that Git remote: $ git fetch [alias] Merge a remote branch into your current branch to bring it up to date: $ git merge [alias]/[branch] Transmit local branch commits to the remote repository branch: $ git push [alias] [branch] Fetch and merge any commits from the tracking remote branch: $ git pull === Rewrite history === Apply any commits of current branch ahead of specified one: $ git rebase [branch] Clear staging area, rewrite working tree from specified commit: $ git reset --hard [commit] Remove previous commit: $ git reset --hard HEAD~1 Note: add "--force" flag to delete from remote repo. === Temporary commits === Save modified and staged changes: $ git stash List stack-order of stashed file changes: $ git stash list Write working from top of stash stack: $ git stash pop Discard the changes from top of stash stack: $ git stash drop === Ignore files and paths === Save a file with desired patterns as ".gitignore" with either direct string matches or wildcard globs: ~~~ logs/ *.notes pattern*/ ~~~ === Git LFS === Install (varies by distribution): # apt install git-lfs $ git lfs install Track file: $ git lfs track "design-resources/design.psd" Check configuration: $ cat .gitattributes List tracked files: $ git lfs ls-files To commit files tracked by LFS, you need to include both your file and ".gitattributes", for example: $ git add .gitattributes $ git add design-resources/design.psd $ git commit -m "Add design file"