WordPress Team Workflow

To help understand Git I like to think of the branches like thumb drives and commits like your saving your updates to that thumb drive. When you create a branch, its like creating a disposable thumb drive, then you add your files to the thumb drive but first you have to commit them to save them.

When you want to got back to that project or version of a project you just checkout that branch, and voila, your files are back to that point. So at a basic level Git is just a way to store and version your projects on a bunch of virtual thumb drives.

Push Project To Github From Local CLI

This is a very efficient workflow for using GitHub. Once you have your project with a README.md file and are ready to push to GitHub.

Before pushing, add a .gitignore file to your root folder with contents like this:
node_modules

  1. In the command line, navigate to the root directory of your project.
  2. Initialize the local directory as a Git repository.
    git init -b main

    Creates the .git folder and sets the branch to main.

  3. Stage and commit all the files in your project.
    git add .
    Check the status to see files to be staged.
    git status
    Then commit the staged files.
    git commit -m "initial commit"

    The -m flag stands for message.

    If you are not automatically logged into the CLI add your user name and pass. If password does not work, then use your access token as the password.

  4. Go to GitHub, create a new Repository, and copy the link / remote repository URL.
  5. Go back to your local CLI and add the new repository / remote repository URL to your project.
    git remote add origin https://github.com/...

    Gives your local git repository a connection to the remote repositories hosted on the GitHub Enterprise Server.

  6. Finally, push your project to GitHub on the main branch
    git push -u origin main

    The -u flag adds an upstream (tracking) reference.

Change Previously Committed Message

You accidentally committed the wrong message to your local Git like this:
git commit -m "Whoops wrong message" and didn't push to the server yet.

git log to view commits. To escape out of logs on a Mac press "shift+Z shift+Z" (capital Z twice) on a PC press "q".

  1. To modify the message, we do another commit with the --amend option and the updated message.
    git commit --amend -m "New message"

    If we do a git log again we can see the message for that commit has changed. Note - changing the commit message will also change the commit hash. When the hash changes the git history also changes.

Manage a Merge Conflict

So you go to commit and there is a merge conflict, here what to do.

  1. First use reset HEAD~1 to keep the changes in your working tree but not on the index. So if you want to "redo" the commit, you will have to add the changes before committing.

    Use ~ to go back a number of generations and 1 for the latest.

  2. Then use stash to temporarily shelves (or stashes) changes you've made to your working copy.
  3. Now you can pull from your branch that will fetch and download the content from a remote repository and immediately update the local repository to match that content.
  4. Now stash pop to pull the most recent stash from history, makes the appropriate changes to files in the local workspace and then deletes that entry from the stash history.

Now you can add ., commit, push as you normally would.

git reset HEAD~1
git stash
git pull origin BRANCHNAME
git stash pop

Create Short Cut Aliases

There is a global .gitconfig file that git put in the root directory of your computer, that apply to all your projects. You can see this file by running cat .gitconfig from the root level of you computer. This file is useful to check your login settings, create aliases and more.

To open and edit the .gitconfig file in VS Code, run code .gitconfig from your machines root directory.

Here is my quick list of some useful Aliases to help remember git commands and speed up workflow.

[alias]
# check status - condensed
st = status -s -b
# add and commit
com = commit -a -m
# amend last commit
oops = commit --amend
# checkout a branch
ch = checkout
# check what branch you are on
br = branch
# unstash changes
unstash = stash pop
# merge master into current branch
update = merge master
# show how many commits ahead/behind we are
howmany = rev-list HEAD --count
# delete a branch from remote repo
rdelete = push origin --delete
# a concise log
logs = log --oneline --no-merges
# delete local branch
ldel = branch -delete
# delete remote branch v 1.0
rdel = push -d origin

You can also use -help in your commands like git .gitconfig -help to help find what your looking for.

Conclusion

Productive programmers tend to be really good at Git. As a developer, one of the best pieces of advice I can give another developer is "Get Good at Git". I use it to maintain and deploy my projects, as well as collaborating in a team environment.

Resources