Git
Commit Tips
Standards conventions of commit messages:
- Must be in quotation marks.
- Written in the present tense.
- Should be brief, 75 characters or less.
Glossary
git checkout HEAD filename: Discards changes in the working directory
git reset HEAD filename: Unstages file changes in the staging area
git checkout SHA: Used for back to previous commit or specific
git branch: List all a Git projects branches
git branch branch_name: Create a new branch
git checkout branch_name: Used to switch from one branch to master or from one branch to another branch
git init: Creates a new git repository
git status: Inspects the contents of the working directory and staging area
git add: Adds files from working area to the staging area
git diff: Shows the difference between the working area and staging area
git commit: Permanently stores file changes from staging area in repository
git log: Shows lists of all previous commits
git branch -d name_branch: Delete non active branch
git branch -m <oldname> <newname>: Rename non active branch
git branch -m <newname>: Rename active branch
Change Remote Origin
git remote set-url origin git://new.url.hereDelete Local and Remote Branch
git push origin --delete <branch_name>
git branch -d <branch_name>Workflow
- WORKING AREA: Make changes to: +additions, -deletions, modifications
- STAGING AREA: Bring changes into the staging area
- REPOSITORY: Save changes to the repository as a 'commit'
How to Merge?
Use git merge <branch_name> command to join file changes from one branch to another.
Example: There are two branches: dev and main. I want to merge my commits from dev branch to main branch. Here are step by step:
- Make sure you're in
devbranch. - Use
git checkout maincommand to move intomainbranch. - Use
git merge devcommand to merge fromdevbranch intomainbranch.
Rewind Commits
I have 5 git commits. I would like to move from the latest commit to the third commit. Then, I want to use that third commit as a HEAD->main branch origin. Please tell me step by step.
- Check your current git commit position with command below.
git log -n 5 --onelineThis will show you the last 5 commits with their hash IDs.
- Move to the third commit:
git checkout HEAD~2This moves you 2 commits back from the current HEAD.
- Create a temporary branch at this commit (optional, but recommended).
git branch temp_main- Switch to the new branch.
git checkout temp_main- Force update the
mainbranch to point to this commit.
git branch -f main temp_main- Switch to the main branch.
git checkout main- Update the remote (origin) to match your local
mainbranch.
git push origin main --forceWarning: This is a force push and will overwrite the remote branch. Make sure you're understand the implications and have backups.
- Delete the temporary branch (if you created one).
git branch -d temp_mainAfter these steps, your local and remote main branch will be pointing to what was previously the third commit from the latest.