Home Using Git on macOS
Post
Cancel

Using Git on macOS

Git on macOS

Before proceeding, ensure that you have your SSH properly setup and configured. Using SSH Keys tutorial coming soon.

Xcode

Install Xcode by running this command in your terminal

1
xcode-select --install

Using Homebrew

You should already have Homebrew installed. Using Homebrew tutorial coming soon

Run the following command to install Git

1
brew install git

The manual way

  • Download the most recent release from here
  • Install GitHub Desktop and launch it
  • Click GitHub Desktop in the top left corner of your screen then select Install Command Line Tool, this will allow you to use GitHub from command line instead of a GUI.

Verification

Verify you can connect to GitHub using your SSH Key.

1
ssh -vT git@github.com

If you are connecting for the first time, you will get this message, type yes then hit enter to proceed.

1
2
3
# The authenticity of host 'github.com (207.97.227.239)' can't be established.
# RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx.
# Are you sure you want to continue connecting (yes/no)?

You may see other things but you should get a line that says something like this:

1
Hi username! You've successfully authenticated, but GitHub does not provide shell access.

Don’t worry about the shell access thing, you don’t want that anyway.

Configuring your Git Profile

  • The below commands will configure your setting for ALL your git repos.
  • To make them work on each repo individually run them without the --global flag. You will need to repeat these steps for each repo you work on.
    • Global is good if you only have one account you work in git with.
    • If you have more than one account, then non-global lets you configure each repo with its respective account.
1
2
3
4
git config --global user.name "Full Name"
git config --global user.email "username@domain.com"
git config --global core.editor vim
git config --global color.ui auto
  • Remember to switch vim above to your editor of choice.

Using Git

  • clone repo so that origin is defined
1
git clone git@github.com:org/repo_name.git
  • load into your repo
    1
    
    cd repo_name
    
  • add your fork
    1
    
    git remote add username git@github.com:username/forked_repo.git
    
  • print remote to verify it’s setup correctly
    1
    
    git remote -v
    
    • You should see something like this (the order may be different for you)
      1
      2
      3
      4
      
      origin	git@guthub.com:org/repo_name.git (fetch)
      origin	git@guthub.com:org/repo_name.git (push)
      fork	git@guthub.com:username/forked_repo.git (fetch)
      fork	git@guthub.com:username/forked_repo.git (push)
      
  • New Branch/PR:
    1
    
    git checkout -b my_new_branch
    
  • rebase with origin/main if outdated
    1
    
    git rebase origin/main
    
  • make changes and push branch
    1
    
    git push <fork> my_new_branch
    

Older good ways to do the same as above

Update Git Repo Fork with Master

This is done from a terminal.

Browse to the cloned repository you want to update and proceed with the following:

  • Verify
    1
    
    git remote -v
    
  • Specify a remote upstream repo to sync with your fork:
    1
    
    git remote add upstream <repo_url>
    
  • Verify:
    1
    
    git remote -v
    

Update Git Repo Fork with Origin Master

  • Fetch branches and commits from the upstream repo. You’ll be storing the commits to master in a local branch upstream/master:
    1
    
    git fetch upstream
    
  • Checkout your fork’s local master, then merge changes from upstream/master into it.
    1
    2
    
    git checkout master
    git merge upstream/master
    
  • Push your updates up to your fork
    1
    
    git push origin master
    

Rebase a Pull Request with Master

Warning: Not sure about the following entirely, it was sent to me from a co-worker but I’ve not used it yet.

  • Fetch branches and commits from the upstream repo. You’ll be storing the commits to master in a local branch upstream/master:
    1
    
    git fetch upstream
    
  • Checkout your fork’s local master, then
    1
    
    git checkout master
    
  • merge changes from upstream/master into it.
    1
    
    git remote update --prune
    
  • Push your updates up to your fork
    1
    
    git fetch upstream
    
  • Merge upstream master
    1
    
    git merge upstream/master
    
  • Checkout the branch you were working on
    1
    
    git checkout branchName
    

Updating a feature branch

Important: Do this after you have updated your Git Fork with the Origin Master. (see instructions above)

  • Check out the branch you want to merge into
    1
    
    git checkout <feature-branch>
    
  • Merge your (now updated) master branch into your feature branch
    1
    
    git merge master
    

Depending on your git configuration this may open vim. Enter a commit message, save, and quit vim:

  • Press a to enter insert mode and append text following the current cursor position.
  • Press the esc key to enter command mode.
  • Type :wq to write the file to disk and quit.

  • This only updates your local feature branch. To update it on GitHub, push your changes.
    1
    
    git push origin <feature-branch>
    

Cheatsheet

This is not an exhaustive list of what Git can do, but rather the more common things you may come across in your daily use.

Create Repositories

Start a new repository or obtain one from an existing URL

  • Create a new local repository with the specified name
    1
    
    git init [project-name]
    
  • Download a project and its entire version history
    1
    
    git clone [url]
    

Making changes

Review edits and craft a commit transaction

  • List all new or modified files to be committed
    1
    
    git status
    
  • Show file differences not yet staged
    1
    
    git diff
    
  • Snapshot a file in preparation for versioning
    1
    
    git add [file]
    
  • Show file differences between staging and the last file version
    1
    
    git diff --staged
    
  • Unstage a file, but preserve its contents
    1
    
    git reset [file]
    
  • Record file snapshots permanently in version history
    1
    
    git commit -m "[descriptive message]"
    

Group Changes

Name a series of commits and combine completed efforts

  • List all local branches in the current repository
    1
    
    git branch
    
  • Create a new branch
    1
    
    git branch [branch-name]
    
  • Switch to a specified branch and update a working directory
    1
    
    git checkout [branch-name]
    
  • Combine a specified branch’s history into the current branch
    1
    
    git merge [branch]
    
  • Delete a specified branch
    1
    
    git branch -d [branch-name]
    

Refactor Filenames

Relocate and remove versioned files

  • Delete a file from the working directory and stages the deletion
    1
    
    git rm [file]
    
  • Remove a file from version control but preserves the file locally
    1
    
    git rm --cached [file]
    
  • Change a file name and prepares it for commit
    1
    
    git mv [file-original] [file-renamed]
    

Suppress Tracking

Exclude temporary files and paths

  • A text file named .gitignore suppresses accidental versioning of files and paths matching the specified patterns, examples:
    1
    2
    3
    
    *.log
    build/
    temp-*
    
  • List all ignored files in a project
    1
    
    git ls-files --other --ignored --exclude-standard
    

Save Fragments

Shelve and restore incomplete changes

  • Temporarily store all modified tracked files
    1
    
    git stash
    
  • Restore the most recently stashed files
    1
    
    git stash pop
    
  • List all stashed changesets
    1
    
    git stash list
    
  • Discard the most recently stashed changeset
    1
    
    git stash drop
    

Review History

Browse and inspect the evolution of project files

  • List version history for the current branch
    1
    
    git log
    
  • List version history for a file, including renames
    1
    
    git log --follow [file]
    
  • Show content differences between two branches
    1
    
    git diff [first-branch]...[second-branch]
    
  • Output metadata and content changes of the specified commit
    1
    
    git show [commit]
    

Redo Commits

Erase mistakes and craft replacement history

  • Undo all commits after [commit], preserving changes locally
    1
    
    git reset [commit]
    
  • Discard all history and changes back to the specified commit
    1
    
    git reset --hard [commit]
    

Synchronize Changes

Register a repository bookmark and exchange version history

  • Download all history from the repository bookmark
    1
    
    git fetch [bookmark]
    
  • Combine bookmark’s branch into current local branch
    1
    
    git merge [bookmark]/[branch]
    
  • Upload all local branch commits to GitHub
    1
    
    git push [alias] [branch]
    
  • Download bookmark history and incorporates changes
    1
    
    git pull
    
This post is licensed under CC BY 4.0 by the author.