[toc]
- Learn Atlassian Git tutorial from the ref link
- Git Reference - Git
- GitHub Git Cheat Sheet - GitHub
- Git Cheatsheet - NDP Software
- Pro Git Online Book- Git
- How to Set Up Git for the First Time on macOS - freeCodeCamp
- How to upgrade Git on Mac (OSX) - Ajahne GitHub IO
- Create Useful .gitignroe Files for Your Project - gitignore.io
- Git Ignore and .gitignore - W3Schools
- Git reset - Atlassian
- Git Tutorials - Atlassian
- Git rebase
-
To replace the built-in Git on macOS, install the Git from Homebrew:
brew install git
-
Change the local path to the Homebrew version:
export PATH=/usr/local/bin:$PATH
-
Check the current version:
git --version
-
Fetch the newest version of Homebrew and all formulae:
brew update
-
Upgrade Git:
brew upgrade git
Path: ~/.gitconfig
-
Set up the global user name:
git config --global user.name "username"
-
Set up the global user email:
git config --global user.email "[email protected]"
-
Check the global configuration:
git config --global --list
git config --global --get <config_name>
Path: repo_path/.git/config
-
Set up the local user name:
git config --local user.name "username"
-
Set up the local user email:
git config --local user.email "[email protected]"
-
Check the local configuration:
git config --local --list
git config --local --get <config_name>
In Git, the system-level configuration refers to the configuration settings applied globally across all users on a system. These settings are stored in a system-wide configuration file and are typically shared among all users on a machine. The system configuration is the broadest level of Git configuration and affects all repositories on the system.
-
Check the system configuration:
git config --system --list
git config --system --get <config_name>
Files that are untracked or tracked by Git.
Files that have been changed but not staged yet.
Files that have been marked for the next commit.
Files that have been committed to the repository.
Files that have been temporarily saved.
Usage:
git stash [save <message>]
: stash the current changes, and revert the current working directory to the state of the last commit.git stash list
: show a list of stashes with their names and messages.git stash apply [<stash>]
: apply the stash.git stash pop [<stash>]
: apply and drop the stash.git stash drop [<stash>]
: drop the stash.git stash clear
: drop all stashes.
Files that are intentionally ignored by using .gitignore
.
- Create a
.gitignore
inside the repo. - Add the names/patterns of files/directories to be ignored.
- Save the file and commit to the repo.
Git will not track files and folders specified in the .gitignore
, but the .gitignore
itself will be tracked. It can also be created inside a subdirectory to ignore files/folders within the directory.
- Create a
.git/info/exclude
inside the repo. - Add the names/patterns of files/directories to be ignored.
- Save the file and commit to the repo.
It works the same way as .gitignore
but will not be shown to anyone else.
-
Enable
core.excludesfile
option in the global Git configuration:git config --global core.excludesfile ~/<.global_ignore_cfg>
-
Create the
~/<.global_ignore_cfg>
. -
Add the names/patterns of files/directories to be ignored.
-
Save the file.
The ~/<.global_ignore_cfg> rules will be applied to all Git repos.
revert
will take a previous commit, recover the working directory based on the selected commit, and save the change in the commit log.
-
Commit by using the default
revert
message:git revert --no-commit/-n <commit>
-
Revert a merged commit:
git revert --mainline/-m <parrent_num> <commit>
reset
will take a previous commit, recover the working directory based on the selected commit, and drop all commits after the selected commit.
git reset [--mixed]
as the default reset operation, changes the HEAD
and index
at the same time.
Before git reset b
:
After git reset b
:
To revert HEAD
and main
:
- Use
git reflog
to search for the reference - Use
git reset [ref]
to move back
git checkout
will only move the current HEAD
to the specific position.
Before git checkout b
:
After git checkout b
:
A Normal HEAD
points to the latest commit of a branch; A Detached HEAD
is pointing to a specific commit other than a branch.
To make changes on a detached HEAD
: create a new branch git checkout -b <branch_name>
before commit.
To link the detached HEAD
to a branch: git switch <branch_name>
.
git commit --amend -m <new_message>
will modify the last commit message and keep anything else the same.
An empty commit allows creating a commit with no changes. It can help trigger certain actions such as CI/CD, or mark one project's history/milestone without introducing actual code changes:
git commit --allow-empty -m <empty_message>