Skip to content

Very Common and Very Uncommon Git Workflows

Kenneth Kasajian edited this page Feb 3, 2017 · 16 revisions

This page assumes the reader is an experienced user of git.

Very Common Workflows

I'm in a folder tree that I want to use with git. From the root I initialize:
git init
I want to see what changes I've made:
git status -uall
I want to see a diff of the changes I've made, before staging:
git difftool -d
After making some changes, I want to stage and commit the changes locally:
git add -A
git commit -m "Stuff I just did comment"
Look through historical log:
git log --oneline --date-order --graph --d -10
I want to see a diff of the changes I've made, after staging:
git difftool -d --cached
After making some changes, amend the last local-only commit to include the changes:
git commit --amend
Undo the last local-only commit:
this moves all the files in the commit to the workspace and moves the head to the parent commit
 git reset --soft HEAD^
Connect a local-only repository to a remote repository:
git remote add origin <url-to-remote>
git push -u origin master
Reference <url-to-remote> can look something like this:
https://github.com/<username>/<repository name>.git
Delete all files not tracked with git. Result should be like a fresh clone:
git clean -xfd
I want to squash and merge from my bugfix branch onto master:
 git checkout master
 git merge --squash bugfix
 git commit

######This will take all the commits from the bugfix branch, squash them into one commit and then merge it with your master branch. ######If you want to keep references to the old commit messages you can write git commit (without -m param) and you will get to modify a drafted commit message containing all commit messages that you squashed

Very Uncommon Workflows

Init a repository where the git files are stored somewhere other than the .git folder:
git init --separate-git-dir ./git
Push all local branches, including ones that aren't tracked:
git push --all origin -u
I'm on a private branch. master is behind origin. I want to switch to master and pull latest but for performance reasons, I don't want the working directory to switch to a stale master only to be immediately replaced with what's on origin.
While in the other branch:
git fetch
git checkout -B master origin/master
I'm on master, and I want to squash all of my local commits since origin into a single commit before doing a push.
before doing a fetch or pull:
git rebase -i origin/master
Locate the Commit in question:
git reflog

or

git fsck --lost-found
To rename a branch on remote:
git push origin origin/<old_name>:refs/heads/<new_name> :<old_name>
Show commits that contain the provided string in the commit content itself.
git log -p --grep "nasty bug"
Show commits that contain the provided string in the commit content itself.
git log -p -S 'Date.new'
Show commits that contain the provided string in the commit content itself, through all branches:
git log -S 'populate_database' --all
Publish / Unpublish branch
publish / send branch to remote:
git push -u origin <branch_to_publish>
unpublish / delete a remote branch:
git push origin :<branch_to_publish>

or

git push origin --delete <branch_to_publish>
Determine which branch contains a commit
git branch --contains <commit>

or

git reflow show --all | grep <commit>

Animated workflow demos

#####Try the animated workflows at:

Other

######The stages of a file in Git

Untracked

'add' --> Unmodified

'edit' --> Modified

'stage' --> Staged

'commit' --> Unmodified

'remove' --> Untracked


References
Clone this wiki locally