code • Git via Terminal CLI - martindubenet/wed-dev-design GitHub Wiki

Git apps  ][  Git via Terminal (CLI)  ][  Terminal command lines (CLI)  ][  Setup Terminal shell  ]

Git via Terminal (CLI)

CLI

Command Line Interface, refers to the Linux Terminal application on macOS.

Bash

Depending on the OS the «Git Bash» term refers to one of the applications included by default with the GitForWindows installer, or the optional «Bash shell» for Linux Terminal used on macOS that was privously the default shell before macOS Terminals. Since macOS Catalina the default shell is now Zsh.

 

Git command lines

Git Description
git clone --filter=blob:none url... ...the initial git clone will download all reachable commits and trees, and only download the blobs for commits when you do a git checkout. This includes the first checkout inside the git clone operation. ...
git stash Discard all local changes, but save them for possible re-use later by running git stash pop command line.
git checkout -- {{FILE.EXT}} Discarding local changes (permanently) to a specific file by replacing it with the latest version from the origin (sources server).
git reset --hard Discard ALL local changes to ALL files permanently.

Many UNDO possibilities are available for local changes. Read this doc from GitLab to learn them in Git.

 

Git flow

git flow

Initialise Git in my project

cd MY_PROJECT
git init

Get the sources

Chronological steps to manage files (when everything goes OK)

git clone …
git fetch
git branch
git checkout {{BRANCH}}

Edit and save your modifications (to GitHub).

git stash
git pull -r origin master
git stash pop
git status
git add -f .
git commit -m 'my comment here'
git push -u origin master
git log

Fetch first!

Fetching makes git retrieve logs from all branches and commits without pulling the files. Just the informations so git is aware of other branches.

Best practice : Fetch before manipulating branches. It stops you from creating duplicates.

git fetch

New local branch

# List your existing local branch
git branch

# Create a new branch and switch to it
git switch -c {{MY_NEW_LOCAL_BRANCH}}

Look for remote branches

git fetch origin

# List remote branches available for checkout
git branch -a

Checkout a new local branch

git fetch
git checkout -b {{MY_NEW_LOCAL_BRANCH}}

Pull a remote branch that does NOT exist on local

git fetch
git checkout -b {{LOCAL_BRANCH}} origin/{{REMOTE_BRANCH}}

Merging branches

The following scenario is merging OTHER_REMOTE_BRANCH within my local CHECKEDOUT_LOCAL_BRANCH:

git merge {{CHECKEDOUT_LOCAL_BRANCH}} origin/{{OTHER_REMOTE_BRANCH}}

Merge branch commit message!?

Please enter a commit message to explain why this merge is necessary,
especially if it merges an updated upstream into a topic branch.

The above message is NOT a Git error message, it’s the editor as git uses your default editor within the Terminal. To proceed simply follow these steps:

  1. Press I key (for Insert)
  2. Write your merge message
  3. Press esc key (Escape)
  4. Type «:wq» (for Write & Quit)
  5. Then press ⏎ Return key (Enter)

Renamed an existing branch

Local branch

Rename a local branch that does not exist yet on the remote (origin) server.

git branch -m {{OLD_BRANCH_NAME}} {{NEW_BRANCH_NAME}}

« -m » is the abbreviation of « Management »

Remote branch

If you get the following error message when trying to git pull a renamed branch.

Your configuration specifies to merge with the ref '{{OBSOLETE_BRANCH_NAME}}'
from the remote, but no such ref was fetched.

— reference on stackoverflow

  1. git checkout {{OLD_BRANCH_NAME}}
  2. git branch -m {{OLD_BRANCH_NAME}} {{NEW_BRANCH_NAME}}
  3. git checkout {{NEW_BRANCH_NAME}}
  4. git branch --unset-upstream
  5. git branch --set-upstream-to=origin/{{NEW_BRANCH_NAME}}
  6. git pull

Push your new branch

Do this if the current branch has no upstream branch

git push --set-upstream origin {{MY_NEW_LOCAL_BRANCH}}

Delete a local branch

  1. Switch to an other branch,
  2. Then delete the branch
    • --delete: Soft delete a merged branch
    • -D: Hard delete
git branch --delete <EXISTING_LOCAL_BRANCH>
git branch -D <EXISTING_LOCAL_BRANCH>

 

Conflicts

Conflict within files

Search for those chains of angle brackets characters to find conflict comments:

<<<<<<< HEAD

>>>>>>>

Got a conflict after stash pop

This king of situation ends up with a « both modified: … » message from git. If you just want to discard local modifications and replace it with the online origin:

git checkout HEAD -- …

 

Revert to previous version

Undo commits (documentation from Microsoft)

  1. Revert a branch to a prior state
  2. Undo the changes made by a shared commit

To revert to a specific commit SHA key or Hash (#) number

  1. Position yourself in the proper branch

    git fetch git switch [YOUR_BRANCH]

  2. Get the latest version of the branch

    git pull

  3. Get the 7 digits Hash number from the logs (ba177fc in this example).

    git log

  4. Reset the index and working tree to the desired tree. Ensure you have no uncommitted changes that you want to keep

    git reset --hard ba177fc

    • OR Move the branch pointer back to the previous HEAD : git reset --soft "HEAD@{1}"
  5. Make that old version new

    git commit -m 'Revert to ba177fc'

  6. Push it

 

Tips & Ticks

How To Write Proper Git Commit Messages

⚠️ **GitHub.com Fallback** ⚠️