Fundamentals 6 min read

Git Basics: Initial Setup, File Operations, Remote Repositories, Branches, and Conflict Resolution

This guide walks through Git's fundamental setup—including configuring username and email, initializing a repository, performing file additions, deletions, commits, managing branches, connecting to remote repositories, and resolving merge conflicts, providing command-line examples for each step.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Git Basics: Initial Setup, File Operations, Remote Repositories, Branches, and Conflict Resolution

Step 1: Basic Git configuration. Set the global username and email:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

View the current configuration:

git config --list

Initialize a new repository:

git init

Step 2: File operations within the repository. Check the status of the working directory:

git status

Add files to the staging area (single file or all files):

git add filename
git add -A

Commit staged changes with a message:

git commit -m "Your commit message"

Delete a file from the working directory and the repository:

rm "filename"
git rm "filename"

Undo changes: revert a file in the working directory to the last committed state:

git checkout -- "filename"

Unstage a file (move from staging back to working directory):

git reset HEAD "filename"

Step 3: Working with remote repositories. Clone an existing remote repository:

git clone "repository_url"

Push local commits to the remote repository:

git push

Alternatively, add a remote manually and push:

git remote add origin "repository_url"
git push -u origin master

Remove an existing remote:

git remote remove origin

Generate an SSH key for password‑less authentication:

ssh-keygen -t rsa -C "[email protected]"

Step 4: Branch management. Create a new branch:

git checkout -b "branch_name"

Switch to an existing branch:

git checkout "branch_name"

Merge a branch into the current branch (typically master):

git merge "branch_name"

Delete a local branch:

git branch -d "branch_name"

Delete a remote branch:

git push -d origin "branch_name"

Push a local branch to the remote repository:

git push origin "branch_name"

List all branches:

git branch

Step 5: Conflict resolution. When merging a branch causes conflicts, Git will indicate the conflicting files. Edit the files to resolve the conflicts, then add and commit the changes:

git add "conflicted_file"
git commit -m "Resolve merge conflict"

If a conflict occurs during a push, first synchronize with the remote repository:

git pull

Resolve any reported conflicts, then commit and push again. Note that attempting to merge without pulling first will fail.

gitconflict resolutioncommand-lineVersion ControlRemotebranching
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.