Git and GitHub are often mentioned together, but they serve different purposes. Without understanding this difference clearly, work can lead to serious workflow problems later on.
Before starting to work with Git, the first and mandatory step is to check whether Git is installed on your system. No Git commands should be run without this check.
git version 2.xx.x and can move to the next step.command not found: git, Git is not installed. In this case, Git should be installed from the official website.git --versionGit associates every commit with a user. Therefore, before using Git, you need to tell Git who the commits are made by. These details:
This sets the name that will appear in commits. It can be your real name, a nickname, or an internal team identifier.
git config --global user.name "Sezer Gec"This sets the email that will appear in commits. It is recommended to use the same email as your GitHub account.
git config --global user.email "example@gmail.com"Git does not work alone. It always works within a folder, which is considered the project directory. All project files reside here. In this step:
mkdir example-project
cd example-projectAt this point, we have a project folder, but Git is not tracking it yet. To make this folder a Git repository, we need to explicitly tell Git to track it. The following command turns the current directory into a Git repository. This process is entirely local and does not require internet connection.
.git is createdgit initAfter initializing the repository, Git can track the folder, but there are no files to track yet. Git only records changes and files.
The next step is to add the first file and save its state to Git history. This process is called a commit.
For example, create a simple README file. This file explains what the project does and is usually present in almost every project.
touch README.mdGit automatically tracks the status of files in the repository. Use the following command to see which files are untracked. At this stage,README.md will appear as untracked.
git statusTo tell Git to include this file in the next commit, add it to thestage area. To add a specific file: git add README.md
git add .Files added to the stage are now ready to be committed. A commit saves the current state of the project to Git history.
git commit -m "Initial commit"At this stage, the local Git repository is pushed to GitHub. GitHub is the platform where the code is stored and shared online.
After logging into GitHub, click New and follow the rules to create a remote repository.
At this stage, the local Git repository is connected to the empty GitHub repository. After this, code can be pushed to GitHub.
Using the GitHub repository URL, a remote called originis defined. origin represents the main repository on GitHub.
git remote add origin https://github.com/USERNAME/example-project.gitVerify that the remote has been added correctly. If everything is correct, origin will appear in the list.
git remote -vAt this stage, the local branch is aligned with GitHub standards and code is pushed for the first time. After this, local and remote repositories are synchronized.
mainSome systems may use master as the default branch. This command renames it to main.
git branch -M mainPush the local main branch to GitHub. The -uflag sets it as the default tracking branch. After a successful push, project files are visible on GitHub.
git push -u origin mainBy default, Git tracks all files in the project folder. However, some files should not be sent to GitHub. To ignore these files, use a .gitignore file.
Create a file named .gitignore in the root of the repository.
touch .gitignoreBelow is a common example for Node.js projects. You can add files or folders you do not want to push to GitHub.
node_modules/
.env
.next/
dist/
build/
.DS_Store
npm-debug.log*
yarn-debug.log*
yarn-error.log*Once the repository is connected to GitHub, Git becomes a natural part of the daily development process. Every change follows a sequence and is pushed to GitHub. This workflow is standard for individual and team projects.
Any changes in project files are automatically detected by Git, but remain local until committed.
Use the following command to see which files have been modified. Modified files appear as modified.
git statusTo commit changes, they must be added to the stage area. Usually, all changes are added.
git add .Changes are saved to Git history with a clear commit message explaining what was done.
git commit -m "Update README content"Commits are sent to the GitHub repository. Since upstream is set, only git push is needed.
git push origin mainSteps to download a GitHub project to your local machine or synchronize an existing project with the latest version on GitHub. Use git clone for the first download, and git pull to keep it up to date.
Used to download a repository from GitHub for the first time. This retrieves all files and commit history to your local machine.
git clone https://github.com/USERNAME/example-project.gitUsed on an already cloned project to fetch changes from GitHub.
git pull origin main