Skip to content

Very Common and Very Uncommon Git Workflows

Kenneth Kasajian edited this page May 5, 2021 · 16 revisions

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

This assumes git and git tools (differ, merger) are configured. If not, see Git - General Prerequisites

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 -u

Without the -u, you won't see files inside of new directories

I want to see a diff of the changes I've made, before staging:

git difftool -d

This should launch something like WinMerge, or whatever you have configured.

After making some changes, I want to stage and commit the changes locally:

git add -A
git commit -m "Stuff I just did comment"

These two commands are probably used 100 times more than any other command.

Look through historical log:

git log --oneline --date-order --graph --d -10

I suggest using "alias.logs". See Git - General Prerequisites. then you get just type git logs -10

I want to see a diff of the changes I've made, after staging:

git difftool -d --cached

It's not that common, but good to remember you can do this.

After making some changes, amend the last local-only commit to include the changes:

git commit --amend

This is great if you want to have a workflow that just periodically "saves" to the local commit, without making history.

Undo the last local-only commit:

 git reset --soft HEAD^

This moves all the files in the last local commit to the workspace and moves the head to the parent commit. It's as if you didn't do a commit.

Do this if you do a commit, and changed your mind.

Note, if doing this from a DOS Command prompt, use HEAD~1 instead of HEAD^. But PowerShell is okay. This is because the ^ is the DOS escape character so you'd have to do ^^ to mean ^, and that's confusing because the number of ^s you pass to git is significant. So to go up 3 commits from HEAD, you'd have to double-up and do HEAD^^^^^^. It's just easier to use the alternate syntax using tilde and type HEAD~3.

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.

git clean -xfd

This is like deleting all temporary files -- it's as if you deleted everything and then did a fresh get.

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 the new stuff that's on origin.

While in the other branch:

git fetch . origin/master:master

which has the same affect as:

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>

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