-
Notifications
You must be signed in to change notification settings - Fork 3
Git commands
This page is a reference for Git commands used in this project. Beginners should start with the Contributor workflow page first to understand how we work.
git clone https://github.com/blomma-dev/pynventory.git
cd pynventorygit checkout master
git pull origin masterThe recommended way is from a GitHub issue:
- Open the issue.
- In the issue sidebar, find Development.
- Click Create a branch.
- Keep the default branch name.
- Copy and run the commands GitHub shows.
Manual branch names use these prefixes:
-
feature/- new functionality -
fix/- bug fixes -
chore/- cleanup, refactoring, documentation
Examples:
git checkout -b feature/search-by-name
git checkout -b fix/delete-confirmation
git checkout -b chore/refactor-functionsCommit often with clear messages:
git add .
git commit -m "add search command with partial name matching"A good commit message:
- has a short subject line (50 characters or less)
- is written in the imperative mood: "add search" not "added search"
- has a blank line, then a longer description if needed
Good example:
fix crash when deleting a product that does not exist
The program would raise an IndexError if you tried to
delete an ID that was not in the table. Now it checks
first and shows a clear message.
Avoid vague messages like "update code" or "fix bug".
git push origin your-branchIf someone merged something to master while you were working, you should update your branch.
Option 1: Merge (recommended for beginners)
git checkout master
git pull origin master
git checkout your-branch
git merge masterIf there are conflicts, fix them, then commit the merge.
Option 2: Rebase (cleaner history)
git checkout master
git pull origin master
git checkout your-branch
git rebase masterRebase rewrites your branch history to place your commits on top of the latest master. This produces a cleaner, linear history.
When to use which:
- Use merge if you are new to Git or unsure. It is safer and easier to undo.
- Use rebase if you want a clean history and are comfortable with Git. Never rebase a branch that someone else is also working on.
A conflict happens when Git cannot automatically combine changes. Here is how to handle it:
- Git will tell you which files have conflicts. Run:
git status- Open the conflicted file. You will see markers like this:
<<<<<<< HEAD
your changes
=======
changes from master
>>>>>>> master
-
Edit the file to keep the correct code. Remove the
<<<<<<<,=======, and>>>>>>>markers. -
Stage and commit the resolved file:
git add path/to/resolved-file.py
git commit -m "resolve merge conflict in resolved-file.py"- If you were rebasing, continue:
git rebase --continueIf you get stuck, ask for help. It is better to ask than to lose work.
Unstage a file (not yet committed):
git restore --staged data/products.dbDiscard changes in a file (not yet staged):
git restore path/to/file.pyAmend the last commit (fix a mistake in your most recent commit):
git add corrected-file.py
git commit --amendThis replaces your last commit with a new one. Only use this on commits that have not been pushed yet.
Undo a commit you already pushed:
git revert <commit-hash>This creates a new commit that undoes the changes. It is safe for shared branches.
Reset to a previous state (dangerous, only on local branches):
git reset --hard <commit-hash>This throws away all commits after the specified one. Never do this on a branch others are using.
Sometimes you need to switch branches but have unfinished changes. Use git stash:
git stashThis saves your changes and gives you a clean working directory. Switch branches, then come back:
git checkout your-branch
git stash popgit stash pop restores your changes and removes them from the stash. If you want to keep them in the stash, use git stash apply instead.
Check your stashed changes:
git stash listIf a rebase goes wrong, git reflog can save you. The reflog records every change to your branch tips, even if commits seem lost.
git reflogYou will see something like:
a1b2c3d HEAD@{0}: rebase: some commit
e4f5g6h HEAD@{1}: rebase (start): some commit
h7i8j9k HEAD@{2}: commit: your last good commit
Find the commit you want to return to, then:
git reset --hard h7i8j9kThis restores your branch to that point. The reflog is your safety net. Do not panic if you lose work during a rebase.
See what files changed:
git statusSee the actual changes:
git diffSee changes in a specific file:
git diff path/to/file.pySee your commit history:
git log --oneline -n 10See what changed in a specific commit:
git show <commit-hash>