Skip to content

Instantly share code, notes, and snippets.

@simonthompson99
Last active September 29, 2022 13:28
Show Gist options
  • Save simonthompson99/8dbf4c09ba5720ba34422874909a7bd7 to your computer and use it in GitHub Desktop.
Save simonthompson99/8dbf4c09ba5720ba34422874909a7bd7 to your computer and use it in GitHub Desktop.
[git workflow] Workflow for common tasks in git #git #workflow

Basics

Command Alias Description
git init <directory> Create empty Git repo in specified directory. Run with no arguments to initialize the current directory as a git repository.
git clone <repo> gcl Clone repo located atonto local machine. Original repo can be located on the local filesystem or on a remote machine via HTTP or SSH.
git config user.name <name> Define author name to be used for all commits in current repo. Devs commonly use --global flag to set config options for current user.
git add <directory> ga/gaa Stage all changes in for the next commit. Replace with a to stage a specific file.
git commit -m "<message>" gc/gcmsg Commit the staged snapshot, but instead of launching a text editor, use as the commit message.
git status gst/gss List which files are staged, unstaged, and untracked.
git log Display the entire commit history using the default format.
git diff gd Show unstaged changes between your index and working directory.
git ls-files . --ignored --exclude-standard --others See untracked files

Rebase - stack commits on top of another commit i.e. you branched off master but master has moved on and you want to incorporate master changes into your current branch.

Undoing Changes

Command Alias Description
git revert <commit> Create new commit that undoes all of the changes made in , then apply it to the current branch.
git reset <file> Remove from the staging area, but leave the working directory unchanged. This unstages a file without overwriting any changes.
git clean -n Shows which files would be removed from working directory. Use the -f flag in place of the -n flag to execute the clean.

Rewriting Git History

Command Alias Description
git commit --amend gc! Replace the last commit with the staged changes and last commit combined. Use with nothing staged to edit the last commit’s message.
git rebase <base> Rebase the current branch onto . can be a commit ID, branch name, a tag, or a relative reference to HEAD.
git reflog Show a log of changes to the local repository’s HEAD. Add --relative-date flag to show date info or --all to show all refs.

Squash last 4 commits into one in interactive rebase - git rebase -i HEAD~4; An interactive text file is displayed. You'll see the word "pick" to the left of each commit. Leave the one at the top alone and replace all the others with "s" for squash, save and close the file. This will display another interactive window where you can update your commit messages into one new commit message.

Comparing Differences

Command Alias Description
git diff <reference> <file> See difference between local file and reference version of file. Reference defaults to HEAD but could be lots of things. Use --cached to compare reference to staged version of file.

Git Branches

Command Alias Description
git branch gb List all of the branches in your repo. Add a argument to create a new branch with the name .
git checkout -b <branch> gcb/gco Create and check out a new branch named . Drop the -b flag to checkout an existing branch.
git merge <branch> gm Merge into the current branch.
git branch --merged <branch> Show which branches have been merged into the branch.

Tags

Command Alias Description
git tag List all the tags in the repo.
git tag -a <tag> Create an annotated tag, e.g. a version.

Remote Repositories

Command Alias Description
git remote add <name> <url> Create a new connection to a remote repo. After adding a remote, you can use as a shortcut for in other commands.
git fetch <remote> <branch> gf/gfo Fetches a specific , from the repo. Leave off to fetch all remote refs.
git pull <remote> gl Fetch the specified remote’s copy of current branch and immediately merge it into the local copy.
git push <remote> <branch> gp Push the branch to , along with necessary commits and objects. Creates named branch in the remote repo if it doesn’t exist.
git push --follow-tags gp Push the branch to the remote and include annotated tags.

Rename a GH Repo:

  1. Rename GH repo in Settings;
  2. Change remote on any cloned repos - git remote set-url origin <new_url_for_repo_same_as_what_would_clone_from>
  3. Rename the local directory accordingly.

Recipes

Catch feature branch up with changes on main (or update a MR if the MR can't be automatically merged):

  1. Make sure both branches are up-to-date
  2. git checkout feature
  3. git rebase maain
  4. Resolve conflicts, then do git add .
  5. git rebase --continue (or use --abort to abort the rebase)
  6. git push --force-with-lease update remote branch provided there haven't been any more commits to remote
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment