
๐ Git Command Central: Mastering Your Private Server!
Alright, intrepid coders! Youโve spun up your own private Git server, you’ve forged your SSH keys, and you’ve made that glorious first git clone. You’re officially a self-hosting hero!
Now, let’s unlock the true power of Git by diving into the commands that will become your daily companions. We’ll cover everything from the everyday essentials to some more powerful techniques that make collaboration and complex projects a breeze. Get ready to commit, push, pull, and merge like a pro!
๐ณ The Absolute Essentials: Your Daily Git Toolkit
These are the commands you’ll use constantly. Master these, and you’re well on your way!
1. git status – Your Project’s Dashboard
This is your best friend. Always run git status to see what’s going on in your working directory. It tells you:
- Which files are modified.
- Which files are staged for the next commit.
- Which files are untracked (new files Git isn’t tracking yet).
- Which branch you’re on.
Bash
git status
2. git add <file(s)> – Staging Your Changes
Before you commit changes, you need to “stage” them. This tells Git exactly which modifications you want to include in your next commit.
- Add a specific file:Bash
git add my_awesome_file.js - Add all changes in the current directory (be careful!):Bash
git add . - Add all changes in a specific folder:Bash
git add src/
3. git commit -m "Your descriptive message" – Saving Your Progress
A commit is like taking a snapshot of your project’s state. It’s a permanent record of your changes.
- Always use a clear, concise message that explains what you changed and why.Bash
git commit -m "feat: Add user login functionality" - Commit staged changes only:Bash
git commit(This will open your default text editor to write a more detailed message.)
4. git push – Sharing Your Work
Once you’ve committed your changes locally, git push sends them to your remote server (your CentOS box!) so others (or your future self) can access them.
- Push to the currently tracked remote branch:Bash
git push - First push on a new branch (sets upstream):Bash
git push -u origin main(-uor--set-upstreamtells Git to link your localmainbranch to the remoteorigin/mainbranch.)
5. git pull – Getting the Latest Updates
If others are pushing to the server (or you’re working from a different machine), git pull fetches the latest changes from the remote and integrates them into your local branch.
Bash
git pull
- Note:
git pullis essentially agit fetchfollowed by agit merge.
๐ฑ Growing Your Skills: Branches and Collaboration
Git’s real power shines in its branching model, allowing fearless experimentation and seamless teamwork.
6. git branch – Managing Your Workflows
Branches let you work on new features or bug fixes without affecting the main codebase.
- List all local branches:Bash
git branch - List all local and remote branches:Bash
git branch -a - Create a new branch:Bash
git branch feature/new-login - Delete a branch (after it’s merged):Bash
git branch -d feature/old-feature(-Dto force delete if unmerged changes exist – use with caution!)
7. git checkout <branch-name> – Switching Focus
Move between branches to work on different tasks.
- Switch to an existing branch:Bash
git checkout feature/new-login - Create and switch to a new branch (common shortcut):Bash
git checkout -b bugfix/fix-typo
8. git merge <branch-name> – Combining Work
When a feature or bug fix is complete on its own branch, you merge it back into a parent branch (like main).
- First, switch to the branch you want to merge into:Bash
git checkout main - Then, merge the feature branch into it:Bash
git merge feature/new-login - Note: You might encounter merge conflicts if the same lines of code were changed differently on both branches. Git will tell you which files have conflicts, and you’ll need to manually resolve them in your editor before committing.
9. git log – Reviewing History
See a detailed history of commits.
- Simple log:Bash
git log - Compact, visual log:Bash
git log --oneline --graph --all - Log with specific file changes:Bash
git log -p my_awesome_file.js
๐ช Advanced Moves: Powering Up Your Workflow
These commands add more finesse and control to your Git game.
10. git remote – Managing Your Servers
Your server is an “origin” remote. You can have multiple remotes.
- List remotes:Bash
git remote -v - Add another remote:Bash
git remote add upstream git@github.com:someuser/someproject.git - Remove a remote:Bash
git remote rm old-remote-name
11. git stash – Temporarily Saving Work
Got unfinished changes but need to switch branches urgently? git stash saves your current work without committing it, leaving your working directory clean.
- Stash your changes:Bash
git stash save "Work in progress on feature X" - Apply the latest stash:Bash
git stash apply - Apply and remove the latest stash:Bash
git stash pop - List all stashes:Bash
git stash list
12. git reset – Undoing Mistakes (Carefully!)
git reset is powerful, but use it with caution, especially with public history.
- Unstage changes (keep local modifications):Bash
git reset HEAD <file>(This moves changes from the staging area back to your working directory.) - Undo last commit (keep local modifications):Bash
git reset --soft HEAD~1(Moves HEAD back one commit, but keeps changes staged.) - Undo last commit and unstage changes (keep local modifications):Bash
git reset --mixed HEAD~1(Moves HEAD back one commit, and unstages changes. This is the defaultgit resetbehavior.) - Completely discard last commit and local changes (DANGEROUS if not pushed):Bash
git reset --hard HEAD~1Danger: This irrevocably deletes your local changes from the last commit. Only use this if you’re absolutely sure you don’t need those changes, and NEVER on commits you’ve already pushed to your shared server.
13. git rebase – Rewriting History (For Clean Branches)
git rebase integrates changes from one branch into another, but instead of creating a merge commit, it reapplies commits from your branch on top of the target branch’s latest history. This creates a linear, cleaner history.
- Rebase your feature branch onto
main:Bashgit checkout feature/your-feature git rebase main - Caution: Never rebase commits that have already been pushed to a shared remote, as it rewrites history and can cause major headaches for collaborators. Only rebase your local, unpushed commits.
๐ Go Forth and Git It!
You now have a robust arsenal of Git commands to manage your projects effectively on your private server. From making your first commit to navigating complex branch histories, you’re equipped for modern development.
Keep experimenting, keep learning, and remember: Git always has your back (as long as you commit often!). Happy coding, and may your repositories always be clean!