GIT (GitHub) — How to configure Git on macOS, connect to a remote repository, create a new branch, and merge into the main branch from the Terminal Command Line.
Git Configuration
Open Terminal first
Then check Git (Exist?)
git --version
If git does not exist install Homebrew first to install git
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Then install git
brew install git
Check Git (Exist?)
git --version
Check git account configuration (exist?)
git config --global --list
If config exits and you want to reset global user name and user email you can use the following commands before define git identity.
git config --global --unset-all user.name
git config --global --unset-all user.email
If git identity / account does not exist, configure it first.
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Check again git account configuration (exist?)
git config --global --list
The Hard Way:
Git Initialize via Terminal (without clone command)
Then, select your folder with cd command
cd desktop
cd yourProjectFolder
Initialize the project folder
git init
Connect to remote repository (you’ve recently created in github or gitlab)
git remote add origin https://git.YourGitAccountName.cloud/YourProjectFolder.git
Check this remote configuration and connection
git remote -v
Pull the files from remote repository
git pull origin main
Add all file to staging
git add .
or add a single file to staging
git add yeni_dosya.txt
Commit it
git commit -m "Yeni"
Push now the changes that you’ve made.
git push origin main
The Easiest Way 1:
Git Clone via Terminal (without initializing)
Type the path in your machine where you want to locate your clone from github repository
cd destkop
mkdir the-project-folder
cd the-project-folder
Clone repository (in the-project-folder)
git clone <repo-bağlantısı>
Create a new file in project folder
touch yeni_dosya.txt
add only this new file in staging area
git add yeni_dosya.txt
or add all file in staging area
git add .
Commit
git commit -m "Yeni dosya oluşturuldu"
Push to main
git push origin main
The Easiest Way 2:
VS Code Clone
If you want you can also use VS Code
Clone Git Repository
Select Github repository
Do some changes or create some new files and commit
Add your changes with the following command below.
git add .
Write your first commit.
git commit -m "testjs"
Push your project with push command below.
git push
— -
If it is required, type the git credentials to be able to push it.
Username for: YourGitUserName
Password for: YourGitUserPassword
— -
That’s all. You’ve just deployed your files successfully.
What if we need to create a new feature?
New Branch
If we need to create a new branch to work on a new feature we can use checkout command to do that.
# Developer 1
git checkout -b feature-branch-1
# Developer 2
git checkout -b feature-branch-2
# Developer 3
git checkout -b feature-branch-3
Create a new feature file.
# Developer 1
touch thenewfeature.js
Add files to staging.
# Developer 1
git add .
Commit it.
# Developer 1
git commit -m "Developer 1: Yeni özellik eklendi"
Push to Feature-branch-1
# Developer 1
git push origin feature-branch-1
After making changes on a feature-branch-1 (e.g., feature-branch-1
), the developer can open a pull request to merge those changes into the main branch.
Here are the steps using GitHub:
- Go to your project’s main page on GitHub.
- Click on the “Pull requests” tab located at the top right corner.
- Click on the “New pull request” or a similar button.
- Choose the
main
branch as the "base" branch and thefeature-branch-1
as the "compare" branch. - Optionally, add a description for your pull request.
- Click the “Create pull request” button.
These steps initiate the pull request, allowing other team members to review the changes made in the feature-branch-1
and, if satisfied, approve the merge into the main
branch.
Other developers can review and provide feedback on the pull request using GitHub:
- Go to your project’s main page on GitHub.
- Click on the “Pull requests” tab located at the top right corner.
- Find the specific pull request you want to review (e.g., the one created by Developer 1).
- Click on the pull request to view the changes, comments, and discussions.
- If everything looks good, other developers can click the “Approve” button to indicate their approval.
Keep in mind that the approval process may vary based on the specific settings and policies established for your project. Some projects might require a certain number of approvals before a pull request can be merged.
or
we can update Main Branch via merge command from terminal (feature-branch-1 to main branch)
# Developer 1, on the local feature-branch-1
git checkout main
git pull origin main
git merge feature-branch-1
git push origin main
This integrates the changes from feature-branch-1
into the main branch.
Getting the Updated Main Branch for Other Developers:
# Developer 2, on the local feature-branch-2
git checkout main
git pull origin main
Other developers use these commands to update their local branches with the changes made to the main branch by Developer 1.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Bonus:
If you need to remove git from your machine.
brew uninstall git
ls /opt/homebrew/etc/gitconfig
rm /opt/homebrew/etc/gitconfig
ls ~/.gitconfig
rm ~/.gitconfig
If does not work.
sudo rm -rf /Library/Developer/CommandLineTools
git --version
—
Bonus:
If you want to remove previously defined remote repositories (github or gitlab) use this command.
git remote remove origin
To rename remote repository use it.
git remote rename origin yeni-origin
—
Bonus:
If you want to check whether github or gitlab repository is used.
git remote -v
- If the URL starts with
https://github.com/
, it's a GitHub repository. - If it starts with
https://gitlab.com/
, it's a GitLab repository.
git log
- Look for the “Author” lines in the commit information.
- This will show the name and email associated with each commit.