User Tools

Site Tools


computing:git

This is an old revision of the document!


GIT


Getting Started

TaskCommand
Start a new repogit init
Clone an existing repogit clone <url>

Configure Git

TaskCommand
Set a config option git config user.name 'Your Name'
Set a config option git config user.email 'Your email'
Set option globally git config –global …
Add an aliasgit config alias.st status
See all possible config options man git-config

Prepare to Commit

TaskCommand
Add untracked file or unstaged changesgit add <file>
Add all untracked files and unstaged changesgit add .
Choose which parts of a file to stage git add -p
Move filegit mv <old> <new>
Delete filegit rm <file>
Tell Git to forget about a file without deleting itgit rm –cached <file>
Unstage one filegit reset <file>
Unstage everythinggit reset
Check what you addedgit status

Make Commits

TaskCommand
Make a commit (and open text editor to write message)git commit
Make a commitgit commit -m 'message'
Commit all unstaged changesgit commit -am 'message'

Move Between Branches

TaskCommand
Switch branchesgit switch <name> OR git checkout <name>
Create a branchgit switch -c <name> OR git checkout -b <name>
List branchesgit branch
List branches by most recently committed togit branch –sort=-committerdate
Delete a branchgit branch -d <name>
Force delete a branchgit branch -D <name>

Diff Staged/Unstaged Changes

TaskCommand
Diff all staged and unstaged changesgit diff HEAD
Diff just staged changesgit diff –staged
Diff just unstaged changesgit diff

Diff Commits

TaskCommand
Show diff between a commit and its parentgit show <commit>
Diff two commitsgit diff <commit> <commit>
Diff one file since a commitgit diff <commit> <file>
Show a summary of a diffgit diff <commit> –stat
git show <commit> –stat

Discard Your Changes

TaskCommand
Delete unstaged changes to one filegit restore <file> OR
git checkout <file>
Delete all staged and unstaged changes to one file git restore –staged –worktree <file> OR
git checkout HEAD <file>
Delete all staged and unstaged changesgit reset –hard
Delete untracked filesgit clean
'Stash' all staged and unstaged changesgit stash

Edit History

TaskCommand
“Undo” the most recent commit (keep your working directory the same)git reset HEAD^
Squash the last 5 commits into onegit rebase -i HEAD~6
Then change “pick” to “fixup” for any commit you want to combine with the previous one
Undo a failed rebasegit reflog BRANCHNAME
Then manually find the right commit ID in the reflog, then run:
git reset –hard <commit>
Change a commit message (or add a file you forgot)git commit –amend

Code Archaeology

TaskCommand
Look at a branch's history git log main
git log –graph main
git log –oneline
Show every commit that modified a file git log <file>
Show every commit that modified a file, including before it was renamed git log –follow <file>
Find every commit that added or removed some textgit log -G banana
Show who last changed each line of a filegit blame <file>

Combine Diverged Branches

TaskCommand
Combine with Rebasegit switch banana
git rebase main

Before

After

Important Files

TaskCommand
Local git config.git/config
Global git config~/.gitconfig
List of files to ignore.gitignore
computing/git.1777199481.txt.gz · Last modified: by oscar