Master Git in 15 Minutes: Essential Commands for Everyday Projects
This guide walks you through installing Git, configuring your identity, creating remote and local repositories, staging and committing changes, managing branches, merging, discarding, rolling back, pushing to and pulling from remote servers, and setting up handy command aliases, all with clear examples.
Workflow Steps
This article demonstrates a complete Git workflow that can be performed on a single machine or across multiple machines.
Install Git
On most Unix-like systems Git is pre‑installed; you can update to the latest development version with: git clone https://github.com/git/git Windows users can download the installer from the official site.
Create a Remote Repository
Register an account on Bitbucket (or GitHub) and create a private repository. After logging in, click the “Create” button and enable the private option.
Configure Git
Set your username and email (preferably the same as your Bitbucket account) with:
git config --global user.name "your_username" git config --global user.email [email protected]Also set the default push behavior:
git config --global push.default simpleCreate a Local Repository
Navigate to your project folder and initialize a repository:
cd ~/workspace/my_site/ git initThis creates a hidden .git directory.
Stage Files
Add all files to the staging area: git add . Or add specific files:
git add my_file my_other_fileCommit Files
Commit the staged files with a meaningful message: git commit -m "initial commit" Check the status at any time with:
git statusCreate a Branch
Create and switch to a new branch: git checkout -b new_feature List all branches:
git branchMerge a Branch
After finishing work on the feature branch, stage and commit any changes, then switch back to master and merge:
git add . git commit -m "adds my new feature" git checkout master git merge new_featureDiscard a Branch
If you want to discard changes, commit them, switch to master, then delete the branch:
git add . git commit -m "feature to be discarded" git checkout master git branch -d new_featureUse -D to force‑delete an unmerged branch.
Delete a Branch
git branch -d new_feature git branch -D new_featureRollback to a Previous Commit
List commits with: git log Checkout a specific commit (using the first 9 characters of its SHA): git checkout 085bb3bcb Or create a new branch from that commit:
git checkout -b my_previous_version 085bb3bcbPush to a Remote Repository
Add the remote origin (replace placeholders with your credentials):
git remote add origin https://[email protected]/your_username/name_of_remote_repository.gitPush the master branch:
git push origin masterClone or Pull a Remote Repository
Clone a repository to a new machine:
git clone https://[email protected]/your_username/name_of_remote_repository.gitOr pull the latest changes into an existing local project:
git pull origin masterAliases
Create shortcuts for frequently used commands, for example:
git config --global alias.c 'commit -m' git config --global alias.co 'checkout' git config --global alias.cob 'checkout -b' git config --global alias.br 'branch' git config --global alias.m 'merge' git config --global alias.a 'add .' git config --global alias.s 'status' git config --global alias.dbr 'branch -d'Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
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.
