Skip to content

goblinhack/git-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 

Repository files navigation

git-cheatsheet

A list of useful git commands

To add all changes to staging

$ git add .

To get rid of untracked files

$ git clean -f -d

To make a branch

$ git branch foo
$ git checkout foo

To make a branch in one step

$ git checkout -b foo

To delete a branch

$ git branch -D my-branch

To list all branches

$ git branch -av

To add a new commit that undoes the given commit

(and does not delete history)

$ git revert <sha>

To go back to an old commit and destroy all future commits (dangerous)

$ git reset <sha> --hard

To merge a branch into master

$ git checkout master
$ git merge my-branch1
$ git merge my-branch2

Now resolve conflicts and then:

$ git add .
$ git commit

To undo a merge

$ git reset --merge HEAD~1

Pushing to a remote repo

$ git push git@github.com:goblinhack/zorbash.git <which-branch>

To avoid typing "git push " always, do:

$ git remote add origin git@github.com:goblinhack/zorbash.git

so we can now do

$ git push origin master
$ git pull origin master

To see where we are pushing and pulling currently to

$ git remote -v
"origin  git@github.com:goblinhack/zorbash.git (fetch)"
"origin  git@github.com:goblinhack/zorbash.git (push)"

To push a branch to the remote

$ git checkout master
$ git pull origin master
$ git checkout -b my-branch1

Now add some files and stuff

$ git commit -m "..."
$ git push origin my-branch1 # you can repeat this add/push

Then do a pull request

Should I use git fetch or pull?

Git fetch only does a download of the latest changes. They are not integrated into your HEAD.

$ git fetch origin

Git pull on the other hand, downloads and integrate and merges with your HEAD.

$ git pull origin master

An alternative to merging is rebase

This replays all commits onto the master and so avoids messy branch history

$ git checkout master
$ git pull origin master
$ git rebase my-branch

To change a commit message (pre push)

$ git commit --ammend -m "my-branch"

To add files to an existing commit (pre push)

$ git add .
$ git commit --ammend -m "my-branch"

Move accidental changes from master to a branch

$ git log # to get SHAs
$ git checkout -b feature-branch
$ git cherry-pick <SHA>
$ git master
$ git reset --soft <SHA> # resets, but leaves  changes in staging
$ git reset        <SHA> # resets, but leaves  changes in working area
$ git reset --hard <SHA> # resets, and removes changes in staging
                       # also removes tracked but leaves untracked files
$ git clean -df          # gets rid of untracked files (handy to remove
                       $ an accidental untar/zip in a git repo)

To see all changes since checking out/cloning

The ‘reflog’ command keeps a track of all changes made in a repository.

$ git reflog

To undelete a branch

$ git checkout -b test-branch
$ git checkout master
$ git branch -D test-branch
$ git reflog
542359ca (HEAD -> master, origin/master, origin/HEAD) HEAD@{1}: checkout: moving from master to test-branch
$ git checkout -b test-branch HEAD@{1}

See just the last commit

$ git log -1 HEAD

A variety of single line output git logs

$ git log --oneline
$ git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all

Get logs after a certain date

$ git log --after="apr 1" --oneline --decorate
$ git log --after="apr 1" --before="yesterday" --oneline --decorate

Get logs for a certain user

$ git log --committer="My name" --oneline

Get only n logs

$ git log -n 3

View just the last commit diffs

$ git log -n1 -p --format=fuller

View all commits diffs

$ git log -p --format=fuller

Recursive submodule diff

$ git diff -a --submodule=diff origin

Look at git commit object SHAS

$ find .git | grep obj | tail -1
.git/objects/13/2fc0aa1789ce26a97b1bed0a9eefa58b12a96e
$ git cat-file -p 132fc0aa1789ce26a97b1bed0a9eefa58b12a96e

Calculate a git hash

$ echo hello | git hash-object --stdin

Get the common ancestor of two branches

$ git merge-base some-branch master

Viewing the diff between two commits

$ git log -n 2 --oneline | tail -2
$ git diff-patch 455f4b22..542359ca

Creating a set of diffs between two commits

Note, place the older commit first

$ git log -n 2 --oneline | tail -2
$ git format-patch 455f4b22..542359ca -o diffs
diffs/0001-add-goblin.patch
diffs/0002-notes.patch

About

List of useful git commands

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published