code • Git via Terminal CLI - martindubenet/wed-dev-design GitHub Wiki
[ Git apps ][ Git via Terminal (CLI) ][ Terminal command lines (CLI) ][ Setup Terminal shell ]
Command Line Interface, refers to the Linux Terminal application on macOS.
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 | 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.
cd MY_PROJECT
git init
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
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}}
git fetch
git checkout -b {{LOCAL_BRANCH}} origin/{{REMOTE_BRANCH}}
Updating to the latest version of master
branch within my local CHECKEDOUT_LOCAL_BRANCH
:
git switch master
git pull
git switch CHECKEDOUT_LOCAL_BRANCH
git rebase master
The following scenario is merging OTHER_REMOTE_BRANCH
within my local CHECKEDOUT_LOCAL_BRANCH
:
git merge {{CHECKEDOUT_LOCAL_BRANCH}} origin/{{OTHER_REMOTE_BRANCH}}
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:
- Press I key (for Insert)
- Write your merge message
- Press esc key (Escape)
- Type «
:wq
» (for Write & Quit) - Then press ⏎ Return key (Enter)
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 »
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
git checkout {{OLD_BRANCH_NAME}}
git branch -m {{OLD_BRANCH_NAME}} {{NEW_BRANCH_NAME}}
git checkout {{NEW_BRANCH_NAME}}
git branch --unset-upstream
git branch --set-upstream-to=origin/{{NEW_BRANCH_NAME}}
git pull
Do this if the current branch has no upstream branch
git push --set-upstream origin {{MY_NEW_LOCAL_BRANCH}}
- Switch to an other branch,
- 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>
Search for those chains of angle brackets characters to find conflict comments:
<<<<<<< HEAD
…
>>>>>>>
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 -- …
- Position yourself in the proper branch
git fetch
git switch [YOUR_BRANCH]
- Get the latest version of the branch
git pull
- Get the 7 digits Hash number from the logs (
ba177fc
in this example).git log
- 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}"
-
OR Move the branch pointer back to the previous HEAD :
- Make that old version new
git commit -m 'Revert to ba177fc'
- Push it