2. Git Commands
Setting up a repository init, clone, config
Saving changes add, commit
Inspecting a repository status, log
Navigate through commit and branch checkout
Undo changes checkout, revert, reset, clean
Rewriting history commit —ammend, rebase (rebase -i), reflog
Syncing remote, fetch, pull, push
Branching branch, checkout, merge
* Need to use with caution, should never be done on public(share) target
17. git revert <commit>
git revert abcd12ef
Before revert
After revert
abcd12ef
- The git revert command undoes a committed snapshot.
- Undo the changes introduced by the commit
- Appends a new commit with the resulting content.
- git revert doesn't alter history
18. git reset
abcd12ef abcd12ef
Before reset
After reset
- Variation:
- git reset <file> : remove from staging area
- git reset : reset branch to most recent commit (soft)
- git reset ——hard
- git reset <commit>
- git reset ——hard <commit>
- git revert is “safe” way to undo
- git reset “dangerous” to undo
** Don’t reset if you’ve already pushed to shared repository
19. revert vs reset
- Don’t alter history
- Safe way to undo but generate
more commit
- For undo shared/public commits
- Alter history
- unsafe way to undo
shared/public commits
- Cleaner history
- For undo private (non-public)
commits
21. git commit --amend
- Modify last commit
- Replace last commit entirely
- Use to fix last commit (which hasn't pushed
to share repo)
- Variation
- git commit --amend
- git commit --amend --no-edit
** Don’t amend commit that you already push to public repository
22. git rebase <base>
- Rebasing is the process of moving a
branch to a new base commit.
- Maintain linear history
git rebase
git merge
23. git reflog
- What to do if you accidentally
- git reset --hard
- Reflog contain log of activity you perform
26. git fetch
- Download commits from remote but not merge to
local
- Give you opportunity to review
- Merge or rebase after review
git fetch <remote>
git fetch <remote> <branch>
27. git pull
git fetch
git merge
git pull = git fetch + git merge
git pull --rebase = git fetch + git rebase
- Download commits from remote and
merge (or rebase) to local branch
28. git push
transfer commits from your local
repository to a remote repo.
git push <remote>
git push <remote> —force
git push <remote> -all
git push <remote> --tags
** Do not use the --force flag unless you’re absolutely sure you know
what you’re doing.
30. git merge
Putting forked history back together
git branch -m <branch>
git branch --no-ff <branch>
fast-forward
(git does this whenever possible)
non-fast-forward (3-ways merge)
(probably after rebase)
31. Cleaner history
- Private branch: rebase & fast-forward merge
- Share: 3-way merge
- Atomic commit and meaningful message
- Good naming convention
32. Staying out of troubles
- Don’t amend commit that you already shared
repository
- Don’t commit your work on a detached head
- Don’t reset if you’ve already pushed to shared
repository
- Don’t use the ‘push --force’ flag unless you’re
absolutely sure you know what you’re doing.
Editor's Notes
Everyday git commands, should be familiar with these commands.
This part will take you through a quick tour.
For those who have use git: a reminder
For those who new: a intro section, show what git is capable of
Git is distributed, all the repos are equal, there is not special repo is set by git, git treat all repo the same
Assign a role to git repo by conventions: we define the roles for those repos, and treat them as such.
Collaboration via sync between repos.
You can create git repo any where, but usually create one and clone
Bare is repo without working copy (share, public repo)
Dev’s repo is non-bare
Dev don’t need create new repo, use clone
Working directory is the dir where your files are in
Staging area is the ‘virtual area’ contains the file (modified, changed) which you want to put in a commit
.git is a folder contain history of your repo & repository-only settings
The git revert command undoes a committed snapshot.
it figures out how to undo the changes introduced by the commit and appends a new commit with the resulting content.