Skip to content

Instantly share code, notes, and snippets.

@adamwespiser
Last active March 2, 2023 18:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adamwespiser/51be2b085540d78b2349 to your computer and use it in GitHub Desktop.
Save adamwespiser/51be2b085540d78b2349 to your computer and use it in GitHub Desktop.
some helpful commands

Useful git commands

General

git-merge-guide.md
ammend to a commit:

		git commit --amend

Remove un-tracked files:

git rm -r --cached .
git add .
git commit -m "some message"
git push

Inspect files:

show file changes throughout git history

git log -p <filename>
git log -p --follow <filename> #if filename changed
git whatchanged <filename>

show who conributed each line:

git blame <filename>
git blame -se <filename> #just commits

Diffs

three states: working, index, HEAD

working to index:

git diff <filename>?

working to HEAD

git diff HEAD <filename>? 

index to HEAD

git diff --cached <filename>?

Branches

list all branches

git branch --list

list remote branches

git branch --r

delete branch (local)

git branch --delete [branchname]

delete branch (remote)

git push origin --delete [branchname]

create new branch

git branch [newbranch]

switch to branch

git checkout [newbranch]

move a branch to a new ref(3 up from head)

git branch -f master HEAD~3

merge back with master

git checkout master
git merge [newbranch]

set a newbanch with remote tracking pull: commits are downloaded into origin/master then merged into master
push: work from master is pushed into origin/master set remote tracking on new branch:

git checkout -b notMaster origin/master
git branch -u origin/master notMaster

push branch a to remote b

git push origin a:b

get commit id of branch:

git rev-parse [branch-name]

be able to push a branch to remote:

git config --global push.default current

Tags

create a tag

git tag website_v0.0.1 6ea7811

push tags to remote:

git push --tags origin

Revert

non-merge commits

git revert --no-commit ..HEAD

merge commits 
git revert -m 1 <COMMIT>

Reset

completely remove the last commit

git reset --hard HEAD~1

View repo info

list all files, staged or cached

git ls-files [--stage,--cache]

view the ref for master

git cat-file -p $(cat .git/refs/heads/master)

Get from remote

usually, you can just use

git pull origin master

if that fails with a merge error, you can use:

git merge --abort # kill merge
git fetch origin master
git diff FETCH_HEAD HEAD # see what is different
git merge [-s [ours,theirs]]? FETCH_HEAD

You can also specify the src&destination for fetching:
(fetch a to b)

git fetch origin a:b

if you have to fix problems after a merge attempt:

git mergetool

to update the remote refs:

git remote update

tell if branch is ahead, behind, or has divered:

git status -uno

show commits in all branches whose name ends in master:

git show-branch *master

more info on checking if pull is needed

Other

use git branch for local changes, then merge with remote origin
use git stash to save working changes
use git tag to mark a ref for future use(versioning)
gitk is a gui
tig is a cmdline tool to view/inspect revision tree

Resource

git help glossary

git from the inside out content of index file git immersion git tools: stasing interactive tutorial

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment