git cheatsheet
Merely a 'blog article' since I want access to these short notes from other places than my home computer
clone, pull, create
git clone <url>
- clones a repo into a newly created directory
- create remote-tracking branches for each branch in cloned repo
- to view them:
git branch --remotes
git fetch
(w/out args) updates all remote-tracking branchesgit pull
(w/out args) in addition merges remote master into current master
- to view them:
git pull <options> <repo>
- runs
git fetch
+ depending on config/options willrebase
/merge
to reconcile diverging branches
git init
- creates empty git repository (creates
.git/
subfolder) git branch -m master
(set default name)git remote add origin <url>
(add remote URL)
basic snapshotting
git add .
- updates index which holds snapshot of working tree
- changes are 'staged for commit'
git status
- displays paths w/ diff between index and HEAD commit
- displays yet untracked (not-added/not-staged) paths
git diff
- w/out options shows diff of working tree and index
git commit -m "message"
- 'commits' stashed changes to the working tree
- create commit containing current contents of index and given message
`git reset stash Notes on rebase: (careful w/ vim from Notes on fully resolving merge conflicts from command line, let's assume:<commit-hash>
(including reverting the tree/index)git rm <file>
git mv <file>
mv
is considered a file deletion + creationgit mv
preserves the git history of the file in question git rm
is does modify the underlying file!)git stash
: saves current local modifications to stash and reverts working dir to HEAD
git stash list
: list stashesgit stash apply
: applies most recent stash and keeps it in stash listgit stash pop
: applies most recent stash and removes it from stash listgit stash apply stash@{2}
: applies specific stash (here stash <2>)git stash branch <new-name> stash@{2}
: creates branch from stashgit stash drop stash@{2}
: remove specific stash from stash listgit stash clear
: clear all stashesbranching, merging
git branch
: list, create, delete branchesgit branch
: lists local branchesgit branch -a
: list all (including remotes)git branch <new>
: creates newgit branch -d <name>
: deletes a branch, branch must be fully merged upstreamgit branch -D <name>
: deletes even if not fully mergedgit checkout <name>
git checkout -b <new>
: creates & checks out (so switches to it)git merge
git pull
pulls in conflicting changesgit log
git tag
tag
creates a clear reference to a certain state (post-unchangeable, as opposed to a standard branch)git tag
: lists tagsgit tag <name>
: creates a taggit tag <name> <HASH>
: creates a tag for a specific commit git log
can be used to inspect hashes of commitsgit tag -a <name>
: creates an annotated tag (more metadata, can be potentially signed as well)git tag -f <name>
: if <name>
is already an existing tag, updates the taggit push origin v1.4
git push --tags
pushes all local tags to remote<feature>
into <master>
means all <feature>
commits are applied one after another on top of <master>
sharing, updating
git fetch
git fetch -all
: all branches and tagsgit fetch -n
: no tagsgit fetch --atomic
: use an atomic transaction to update local refsgit fetch <name>
: fetch specific repogit pull
(mybranch)$ git pull origin/master
merge
by default, rebase
if so set)git push
git push --all
: push all branchesgit push origin master
: pushes local to remote master origin
is the default name of the remote(master)$ git push
synonymous with git push origin master
pull
is needed before pushing(my-branch)$ git push origin master
will not push local my-branch
into remote master
; it will push the local master
to remote master
(which as stated, is even rejected if remote has changes that haven't been applied locally); it's safe and non-destructivegit remote
git remote add
adds a remote git remote get-url --all
will list all URLs for the repogit remote set-url
change urls of the remoteinspection, comparison
git log
: shows the commit logsgit diff
: shows changes between working tree and HEADissues: line endings on Windows
\r\n
but git will usually normalize to \n
when pushing and re-adjust to \r\n
when pulling (normal modus operandus)git bash
however can still lead to whole files showing as conflicted due to a file ending difference (the merge potentially overwrites a whole file)git merge branchB -X renormalize
; from man git-merge
:This runs a virtual check-out and check-in of all three stages of any file
which needs a three-way merge. This option is meant to be used when merging
branches with different clean filters or end-of-line normalization rules.
Resources
merge conflict resolution
git bash
(will use \n
not clrf
)git push
git pull
git status
git diff --name-only --diff-filter=U
vim conflicted_file.txt
add .
)git add . && git commit "resolved" && git push
additional notes
git merge --abort
at any point before you have committed to abort merge completely<<<<<<< HEAD
your local changes
=======
incoming changes
>>>>>>> branch-name