Tecnate | Last Updated: 26 Nov 2024 |
In Git branching nomenclature, main
and master
are synonymous. This guide will use main
.
⚠️ Note: assume that codeblocks in this guide show an ordered sequence of steps to follow unless otherwise specified.
main
branch directly.git checkout development
# Checkout your main branch
git checkout main
# Merge your dev branch into main
git merge development
In general, write comments as present-tense imperatives:
Whether you are using Git or SFTP to deploy changes, the last step is to refresh your database. Settings or content that you add/modify are stored in your database. (E.g., custom post types, custom fields, references to images, etc.)
You may already have Apple Git running on your machine. If you run git --version
and are returned something like “git version 2.24.3 (Apple Git-128)
” and then install Git with Homebrew, your machine will have two different versions of Git at the following locations:
Updating the PATH variable and overwriting any symlinks as shown below will make your machine use the git
command default to the open source version of Git. Using Homebrew will make it easier to update (i.e. upgrade) Git in the future.
# Iterate through all Git executables and display locations/versions
for i in $(which -a git); do echo -n "${i}: "; ${i} --version; done
# Install or upgrade git with Homebrew package manager
brew install git
brew upgrade git
# Update the PATH variable to the Homebrew install
export PATH=/usr/local/bin:$PATH
# Check that your git command is linked to the updated Homebrew version you installed
git --version
# If necessary, overwrite any existing symlinks to the installed version of git
brew link --overwrite git
-f
(force): Force the action (e.g., overwriting changes, deleting branches).
-d
(delete): Delete a branch.
-v
(verbose): Provide more details for certain commands.
-b
(branch): Create or switch to a branch.
-D
(delete force): Force delete a branch.
Press q
after logging to quit and return to terminal entry.
--oneline
: Show each commit as a single line in logs.
--graph
: Show commits in a graphical structure.
-n
: Limit the number of commits to display.
Staging and Files:
-a
(all): Add all modified and deleted files to the next commit.
-m
(message): Add a commit message.
--cached
: Work with files staged for commit but not yet committed.
--hard
: Reset changes and delete them permanently.
--soft
: Reset changes but keep them staged.
-u
(upstream): Set upstream tracking branch.
--amend
: Modify the most recent commit.
--squash
: Combine commits when merging branches.
# Navigate to project directory
cd Sites/projectName
# Initialize repo
git init
# Confirm all files are present
git status
# Add all files and commit with comment
git add . && git commit -m "Initial commit"
# Ensure working tree is clean
git status
# Check for existing remote repos
git remote -v
# In a browser, go to your GitHub account and create new repository
# Make private if necessary
# Switch from HTTPS to SSH
# Copy this command block from GitHub:
git remote add origin git@github...git push -u origin main
# Back in terminal, paste the command in project directory where git was initialized
# Type your SSH password when prompted
# Check remote origin is now connected
git remote -v
# Refresh GitHub page in browser and confirm sync
The
-u
tag ingit push -u origin main
is used to set the origin as the upstream remote in your git config. It is only needed the first time you push.
-u
: equivalent to--set-upstream
- As you push with the
-u
tag option, the local branch (main
) is linked to the remote server (origin) automatically with a matching branch name (main).
View more about Remotes at Git Basics - Working with Remotes
# Checkout the branch you want to connect to remote
git checkout -b development # Option 1: create new branch
git checkout development # Option 2: use existing branch
# Set the upstream of the local branch (development) to the remote server (origin)
git push -u origin development
# View all remote branches
git branch -r
# You should see 'origin/development' listed
To remove Git from a project, you can simply delete the .git directory in the root of your project.
⛔️ Caution! This will permanently remove the version control history and configurations.
# Navigate to project directory
cd Sites/projectName
# View all files in the directory, including hidden files like .git
ls -a
# Remove all git directories and their contents recursively
# BE SURE BEFORE YOU PROCEED!
rm -rf .git
# Remove the .gitignore file
rm .gitignore
# Go to GitHub and delete your repository if you don't want a remote copy
After cloning a remote repo, you may want to remove the tracking so that your local project is no longer synced to the remote.
# Navigate to project directory
cd Sites/projectName
# Remove remote tracking
git remove remote
# View local branches
git branch
# View remote branches
git branch -r
# View all branches
git branch -a
# Change the name of oldBranch to newBranch
git branch -m oldBranch newBranch
# View all branches
git branch
# Create a checkout a new branch called “staging”
git checkout -b staging
# Copy/paste info from the "Clone" btn on GitHub: git@github.com:<userName>/<repoName>.git
git clone git@github.com:userName/example-project.git
# ??? NEEDS INSPECTION
git checkout -b feature/modal origin/deploy
# Push the feature/modal branch to remote origin
git push origin feature/modal
# Copy/paste info from the "Clone" btn on GitHub: git@github.com:<userName>/<repoName>.git
git clone -b branch-name git@github.com:userName/example-project.git
# View existing remotes
git remote -v
# Rename origin to upstream
git remote rename origin upstream
Source: TheServerSide
fetch
: copies changes to you local git repo.
fetch
if you want to pull down the latest changes from a remote repository without overwriting anything in your working directory and merge later.pull
: copies changes directly into your working directory. A pull is equivalent to a fetch & merge.
pull
if your workspace has no uncommitted files, and you want to copy the latest changes from remote directly into your working directory.git pull
short-circuits and becomes git fetch
; all updates are copied to local repo but your local workspace is left alone.# Synchronize branch list
git fetch -p
-p
: prune.
- After fetching, branches which no longer exist on the remote will be deleted.
# Shows commits missing from main but present on devBranch
git log main..devBranch
# Local branches comparison
git rev-list --left-right --count main...devBranch
# Remote branches comparison
git rev-list --left-right --count origin/main...origin/devBranch
# Example Output:
# 1 3
# Compared to main, devBranch is 1 behind & 3 ahead
# Return list of unique commits on each branch
git rev-list --left-right --pretty=oneline main...devBranch
# Example Output:
# <abc123...unique commit 2 on main
# >def456...unique commit 1 on devBranch
<
SHA points to left branch (in this case,main
)>
SHA points to right branch (in this case,devBranch
)
When merging, you checkout the trunk (e.g.
main
) and merge in the development branch (e.g. staging, feature/modal, bugfix, etc).
# Checkout the trunk
git checkout main
# Merge the branch you worked on
git merge development
# Make sure working tree is clean and resolve any conflicts if necessary before pushing.
git status
# Push your merged local (main) to remote (origin)
git push origin main
Perform merge conflict housekeeping in your text editor.
- HEAD points to the current, checked out branch (usually
main
when you’re doing merges).- The
checkout
operation moves the HEAD pointer to the specified commit (i.e. a branch name or a git log ID hash).
# See what HEAD points to
cat .git/HEAD
- Decide which code to keep and delete everything else, including the GIT inserts.
- Only proceed after the conflict is resolved.
# Stage changes (after conflicts are resolved)
git add filename.abc
# Commit changes with comment
git commit -m "Fix merge conflict in filename"
# Make sure working tree is clean
git status
# Push your merged local (main) to remote (origin)
git push origin main
# Discard uncommitted edits & revert a file to prior commit state
git checkout -- filename.abc
# Discard all file changes & revert everything to prior commit
git checkout .
# Remove untracked directories
git clean -fd
# Delete branch that has been already been pushed & merged with remote
git branch -d localBranchName
# Delete branch that has NOT been pushed & merged with remote
git branch -D localBranchName
# View remote branches
git branch -r
# Delete remote branch - Option 1
git push origin --delete remoteBranchName
# Delete remote branch - Option 2
git push origin :remoteBranchName
The remote may have already been deleted if you see this:
error: unable to push to unqualified destination: remoteBranchName The destination refspec...
- Run
git fetch -p
to prune branches that no longer exist.
⚠️ Warning: Resetting is destructive. Make sure you’re ok with losing the data before you proceed!
Git Revert: undo changes made in a commit by creating a new commit that reverses the changes.
# Undo changes introduced by commit hash abc123
git revert abc123
Git Reset: reset the current HEAD to a specified state, which can be a previous commit or the staging area.
# This is a codeblock of options, not sequences
# Replace abc123 with the hash (ID) of the commit you want to keep
git reset abc123 # Reset to specified commit
git reset --soft abc123 # Reset & keep changes staged
git reset --mixed abc123 # (Default) Reset & unstage changes
git reset --hard abc123 # Reset & discard changes
git reset --hard HEAD^ # Undo last commit & all changes
git reset --hard HEAD^^ # Undo last 2 commits & all changes
# Checkout the branch that you want to rollback (i.e. development)
git checkout development
## Reset the checked out branch to match another branch (i.e. main)
git reset --hard main
# Force push the changes to your remote branch if desired
git push origin development --force
# Create a branch for hosting
# You must name it 'gh-pages' for GitHub
git checkout -b gh-pages
# Push branch to GitHub
git push origin gh-pages
# In your browser, visit your GitHub repo
# Click the Settings button (gear icon in the top menu)
# Scroll down to the Pages section to view/modify any settings
See the React For GitHub Pages guide to get static React applications running on GitHub.