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.
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 --listInitialize a new repository:
git initStep 2: File operations within the repository. Check the status of the working directory:
git statusAdd files to the staging area (single file or all files):
git add filename git add -ACommit 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 pushAlternatively, add a remote manually and push:
git remote add origin "repository_url" git push -u origin masterRemove an existing remote:
git remote remove originGenerate 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 branchStep 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 pullResolve any reported conflicts, then commit and push again. Note that attempting to merge without pulling first will fail.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.