-
The 4 levels of the git organization
- Working directory (on local computer): work in text editor and/or an IDE (RStudio, Visual Studio, etc.)
- Staging area: include/save changes to the next commit
- Local repository: commit to project history
- Remote repository on github.com: share code with collaborators and backup local branches
-
Inspecting a repository
- State of working directory and staging area: git status
- History of commits:
git log --graph --full-history --oneline --decorate
- List of branches (* indicates the active branch):
- Local branches:
git branch
- Remote branches:
git branch -r
- List of remote connections:
git remote -v
-
Finding stuff in a repository
- Find all commits which have affected a file:
git log -- *<part_of_file_name>*
- Find the SHA of the last commit that affected a file
git rev-list -n 1 HEAD -- <file_path>
- Find all commits which have deleted files and list the deleted files:
git log --diff-filter=D --summary
- Find all commits which have affected a file:
-
Remove commits from current (private, i.e., not published on remote repository) state of a branch (i.e., re-writing history)
- From working directory, staged snapshot, and commit history:
git reset --hard HEAD~1
- From staged snapshot and commit history:
git reset --mixed HEAD~1
- From commit history:
git reset --soft HEAD~1
- From working directory, staged snapshot, and commit history:
-
Remove commits from current published branch (by creating a new commit, i.e., it does not re-write history):
git revert HEAD~1
-
Restore file from a previous commit:
git checkout <deleting_commit>~1 -- <file_path>
-
Amending commits
-
Amending the most recent commit message: (see SO for alternative scenarios)
- Completely rewrite message from scratch:
git commit --amend
- Amend by starting from old message:
git commit --amend -c HEAD
- Completely rewrite message from scratch:
-
Tag an old commit retroactively: So that the tag's date/time corresponds to the commit's date/time by temporarily setting the tag's clock: fill in appropriate values for
<branch>
,<commit SHA1>
,<version>
, and<message>
git checkout <branch> git reset --hard <commit SHA1> GIT_COMMITTER_DATE="$(git show --format=%aD | head -1)" git tag -a <version> -m "<message>" git push --tags git pull
-
Moving commits from one branch to another: (see SO) example scenario:
<SHA1> -> <SHA2> -> <SHA3> = branch <A> '-> <SHA4> -> <SHA5> -> <SHA6> -> <SHA7> = branch <B>
- Apply commits
<SHA6>
to<SHA7>
to head of branch<A>
git checkout <A> git reset --hard <SHA7> git rebase --onto <SHA3> <SHA5> # resolve conflicts git rebase --continue # respectively, --skip; repeat until done
- Apply commits
-
-
Interruptions to coding
'Stashing' saves uncommitted changes and resets/cleans the working directory, e.g., to switch branches, to pull into a dirty tree, to interrupt the workflow in general. Stashes are handled in the same way as commits by git commands, but they are not linked to a specific branch. Stashes are named <stash@{X}> where X is the number on the stack. For more details see here and here
- Push a new stash onto stack:
git stash
(this will only stash files that are already tracked); to stash also untracked (i.e., new files):git stash --include-untracked
- List stored stashes on stack:
git stash list
- Apply a stored stash:
git stash apply
will apply <stash@{0}>; apply stash with number X:git stash apply stash@{X}
. Git gives merge conflict messages if a stash does not apply cleanly. Apply a stash and stage files as before:git stash apply --index
- Remove a stash from the stack:
git stash drop stash@{X}
- Apply and remove a stash:
git stash pop
- Show what applying a stash would add/remove to :
git diff <branch> stash@{X}
- Push a new stash onto stack: