How to use git command in mac using command line

Install Git Using Xcode

xcode-select --install

With Xcode running on your Mac, you can check whether Git is also available by prompting for the Git version:

git --version

Install Git Using Homebrew

Another way to install Git is with Homebrew, the package management system for Mac.

Run the following brew command in the terminal:

brew install git

Then, check the Git version to verify the installation:

git --version

Install Git Using MacPorts

If you are using MacPorts to manage your packages on the system, you can use the port command to set up Git.

Start by updating MacPorts with the command:

sudo port selfupdate

Search for and install the newest Git ports and variants by running the following two commands:

port search git

port variants git

Then, install Git with:

sudo port install git

Configure Git

The next step is to configure Git by adding your credentials to the system. This is important as it helps keep track of which user is committing changes to a project.

Open the terminal and configure your GitHub username:

git config --global “your_github_username”

Then, add your email:

git config --global ""

Track and Commit Changes

To demonstrate how to work with files on local Git repositories, we are going to create a demo folder and file to work with.

#First, open the terminal and create a new folder named NewFolder.

mkdir /Users/[username]/Desktop/Tools/Git/NewFolder

#Then, move into that directory. The path may differ according to the location where you created the new folder.

cd /Users/[username]/Desktop/Tools/Git/NewFolder/

#As we want to keep track of changes inside this folder, we need to create a local Git repository for it. Running the git init command initializes an empty git repository in this particular location. Therefore, run the command:

git init

# While in the directory NewFolder, type the following command:

git status

This shows the state of the working directory and displays if any changes made inside the directory.

Since the folder we created doesn’t have any files in it, the output responds with: nothing to commit.

#Add some files inside NewFolder and see how the git status changes:

touch file1.txt

#Check the status again:

git status

The output tells you there are untracked files inside the directory and lists file1.txt. Git is tracking the folder in which the file was added, and notifies you that the changes are not being tracked.

#Prompt Git to track the new file by running:

git add test1.txt

If you recheck the git status now, you would see that the file is now being tracked (as it changed from red to green). However, you still need to commit this change.

# Commit all changes and add a message that describes the commit:

git commit -m "added test1.txt"


There are a lot of different ways to use Git. There are the original command-line tools, and there are many graphical user interfaces of varying capabilities.

cat ~/.gitconfig
git clone
git fetch
git branch -all
git branch -r
git branch
git status
git pull origin BRANCH_NAME
git add .
git commit -m"message"
git push origin BRANCH_NAME
git log -7 --oneline
git checkout BRANCH_NAME
git log
git branch -d dev-webcommerce
git merge -m"Merge Message" dev-webcommerce
git diff .
git branch BRANCH_NAME
git checkout -- Folder/Commit ID
git config --global push.default current
git reset --hardclear
git reset --hard
git rm filename
git log --oneline
git stash
git revert commitID