Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Aquamite

macrumors 6502
Original poster
As an iOS developer I started working with Xcode's Git integration back in the years of iOS 13/14. During that time I've had to face lots of problems with it mostly when merging big developments, straight to the point where the other day I had to redo a complete development on a new branch because the merge wouldn't work at all.

To avoid more headaches with Xcode's Git integration I've gone back to basics and have begun to use the Terminal for Git. Yes, using the Terminal is not as visual as Xcode's UI, but I've found it to be way more reliable. Here are my recommendations for using Git with Terminal:


First is thing would be to install Oh My Zsh, which is an open source, community-driven framework that manages your Zsh configuration. It makes Zsh more powerful and friendly and it shows you your current Git branch in the prompt, along with helpful color cues for staged, unstaged, and uncommitted changes. To install just type this:

Then I'd suggest trying to remember the following shortcut for clearing the whole Terminal output for when it gets too messy:
⌘ + ⌥ + L

Now here comes all the commands I've found useful to substitute the use of Xcode's Git integration:


CLONE
Clones a remote repository and automatically sets it as the remote named origin.

• Clone a remote repository
git clone repository_url
Downloads the entire repository from the remote server and creates a local copy on your machine.

• Clone a repository into a specific folder
git clone repository_url folder_name
Downloads the repository and places it into a folder with the specified name.

• Clone a specific branch
git clone --branch branch_name repository_url
Clones the repository and checks out the specified branch instead of the default one.


FETCH
Updates local references to remote branches but does not merge changes automatically.

• Fetch updates from origin
git fetch
Downloads the latest changes from the remote repository without merging them into your current branch.

• Fetch a specific branch from origin
git fetch origin branch_name
Downloads updates for the specified branch without modifying your local branch.

• Fetch all remotes
git fetch --all
Downloads updates from all remote repositories.


BRANCH

• List local branches

git branch
Shows all branches on your machine. The * indicates the branch you’re currently on.

• List all branches (remote + local)
git branch -a
Shows both local branches and branches on the remote repository.

• Create new branch
git branch branch_name
This makes a new branch, but you stay on your current branch.

• Copy an existing local branch into a new local branch (without moving HEAD)
git branch -c branch_name new_branch_name
Creates a new branch with the same content as branch_name without switching to it while HEAD remains on your current branch.

• Delete local branch
git branch -d branch_name
Deletes the branch. Git will prevent deletion if the branch has unmerged changes.SWITCH

• Switch to an existing branch

git switch branch_name
Moves you to the branch you specify.

• Create a new branch from the current one and switch to it
git switch -c branch_name
Makes a new branch based on your current branch and switches to it immediately.


PULL

• Pull current branch from origin

Fetches and merges changes from the remote branch you’re tracking.

• Pull an existing branch from origin
git pull origin branch_name
Fetches and merges updates from branch_name on the remote repository.


PUSH

• Push your current branch to origin

Sends your commits to the remote branch you’re tracking.

• Push existing branch to origin
git push origin branch_name
Uploads your branch to the remote repository.

• First time push (set upstream)
git push -u origin branch_name
Sets the remote branch as the default upstream for future pushes and pulls.


COMMIT

• Commit staged changes with a message

git commit -m "message"
Saves your staged changes to the repository with a description.

• Amend last commit
git commit --amend
Changes the most recent commit (e.g., to fix the message or add missing files).


STATUS

• Check current local branch status

git status
Shows staged changes, unstaged changes, and untracked files.


OPEN

• Open directory in external editor (e.g., Sublime Text)

open -a “Sublime Text” .

• Open file in external editor (e.g., Sublime Text)
open -a “Sublime Text” "File Name"

• Open file in merge tool
git mergetool "File Name"


CHECKOUT

• Keep only “theirs” or “ours” changes

git checkout [--theirs || --ours] "File Name"
Use when resolving merge conflicts and choosing one version.ADD

• Stage a specific file

git add "File Name"

• Stage all files in current directory
git add .

• Stage all changes in the repository (including deletions)
git add -A


REMOVE

• Mark a file as removed and stage the deletion

git rm "File Name"


DIFF

• See differences for a conflicted file

git diff -- "File Name"
Shows what changed before committing.


MERGE

• Abort merge

git merge --abort
Cancels a merge in progress.

• Continue merge when conflicts are resolved
git merge --continue
Completes the merge once conflicts are fixed.STASH

• Save tracked changes and clean working directory

git stash

• Save tracked changes with a message
git stash push -m "message"

• Save tracked + untracked files
git stash push -u

• List all stashes
git stash list

• Apply specific stash
git stash apply stash@{n}

• Apply last stash and remove it from stash list
git stash pop

• Delete specific stash
git stash drop stash@{n}

• Delete all stashes
git stash clear


RESTORE
For restoring files or undoing staged changes without changing branch history so it only affects your working directory or staging area.

• Restore all modified files in directory
git restore .
Discards all local changes in all your files from the working directory without touching commits.

• Restore a specific file to discard all local changes in it
git restore "File Name"
Discards all local changes in the specified file from the working directory. Handy if you want to undo edits.

• Unstage all staged changes
git restore --staged .
Moves all files out of staging keeping the edits in the working directory.

• Unstage a specific file (undo git add)
git restore --staged "File Name"
Moves the specified file out of staging keeping the file edits in the working directory. Handy if you added the wrong file to staging by mistake.


RESET
For changing the branch history by moving the branch pointer to a previous commit. It can also change staged changes and working directory depending on the options.

• Reset staged changes
git reset
Unstages all staged files but keeps the changes in the working directory.

• Reset branch to a previous commit
git reset --hard commit_hash
Resets the current branch to the specified commit clearing the staging area and discarding any working directory edits. Warning: This rewrites branch history and permanently removes uncommitted work.
 
If you don't mind using the terminal, I recommend giving lazygit a try. I only used it couple times myself but so far it seems to be quite a nifty tool. Although I wish it came with a text manual instead of linking you to a youtube video. UI is straightforward enough (press ? on keyboard for guide and every pane in the window is marked with a number and there are commonly used commands on the bottom of the screen akin to the nano editor)
 
Never understood the point of GUIs for git anyway. The terminal interface is nice and you have control. Nothing happens behind your back like the GUI does.
I've used Xcode and a variety of other IDEs for well over a decade at this point and I've never liked a single GUI for git.

I also don't see the point in Oh My Zsh - It's probably a perfectly fine setup, (although I don't like the suggestion way of installation of just running shell curled down straight up - Huge security vulnerability), but you can achieve a lovely shell super easily without it. This here is without it just using my own ~/.zshrc file
1773340823692.png
Beautiful, clean, has your git details and everything.

As I said, I'm sure Oh My Zsh is a great project, I just see it as a bit of an unnecessary dependency
 
Never understood the point of GUIs for git anyway. The terminal interface is nice and you have control. Nothing happens behind your back like the GUI does.
I've used Xcode and a variety of other IDEs for well over a decade at this point and I've never liked a single GUI for git.

I also don't see the point in Oh My Zsh - It's probably a perfectly fine setup, (although I don't like the suggestion way of installation of just running shell curled down straight up - Huge security vulnerability), but you can achieve a lovely shell super easily without it. This here is without it just using my own ~/.zshrc file
View attachment 2612794Beautiful, clean, has your git details and everything.

As I said, I'm sure Oh My Zsh is a great project, I just see it as a bit of an unnecessary dependency
I really prefer managing things like cherry picks , committing only certain changes in a file, merge conflicts etc through a GUI inside the IDE

Otherwise I am personally fine with just git commands.
 
I really prefer managing things like cherry picks , committing only certain changes in a file, merge conflicts etc through a GUI inside the IDE
Each to their own but I always find myself confused by the IDE in those cases. Prefer just git add -p for taking specific parts, and cherry-pick --no-commit followed by add -p to achieve the same thing for a cherry-pick, although I basically never find myself not wanting to cherry-pick the whole commit
 
  • Like
Reactions: Aquamite
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.