Now that you're more comfortable with git, it's time to take it to the next level with more advanced git commands
- Initialise a new git repository
git init
- View modifications made in working dir / View staged files
git status
- Undo modifications to file
git checkout <file name>
- Undo all modifications in working dir
git checkout .
- View details of local changes
git diff
- Add change to staging area
git add
git add -p
- Undo staging changes from specified file
git reset <file name>
- Undo all staged changes
git reset .
- Commit changes in staging to the local repository
git commit
git commit -m "commit message here"
- Show details of a commit
git show <commit-sha>
- Show git commit history list
git log
- Show all branches of commit history title in a graph
git log --all --oneline --graph
- View graphical UI of commit history graph and commit details
gitk
- Make changes to the last commit (eg: add or remove file, edit commit message)
git add .
git commit --amend
- Make changes to the last commit (eg: add or remove file, Do not edit commit message)
git add .
git commit --amend --no-edit
- Undo commits to a certain point in time (changes from commit dumped to working dir)
git reset <commit SHA to reset to>
- Delete commits to a certain point in time (Warning: also deletes any uncommitted changes)
git reset --hard <commit SHA to reset to>
- Rewrite commit history with rebase interactive
- decide what group of commits you would like to rewrite, pick the SHA just before that
- `git rebase -i
- pick the commit if you would like no changes to it
- drop the commit if you would like to delete it
- edit the commit if you would like to amend it
- squash the commit if you would like it to squash to the previous commit
- For further details on rebase interactive please visit Git-Tools-Rewriting-History
As we should never modify commits which are already on remote, we can only revert it with a new commit.
Create a new commit which is the exact opposite of specified commit SHA
git revert <SHA>
eg: when you need to pause the feature you are working on, to work on something else urgent
Saves changes from working dir and staging to stash area
git stash
Saves changes from working dir and staging area and new files, to stash area
git stash -u
Save stash with a description
git stash save my-awesome-stash
Remove last stashed changes and dump them to the working dir
git stash pop
Remove second last stashed and dump to the working dir
git stash pop stash@{1}
Dump last stashed to the working dir, but leave stash intact
git stash apply
List all stashed changes
git stash list
Best practices: Do a git pull to get the lastest code before creating a branch
Create branch
git branch <branch-name>
Switch working dir to specified branch
git checkout <branch-name>
Create branch and switch to branch (2-in-1 command)
git checkout -b <branch name>
Delete specified branch
git branch -d <branch-name>
Force delete specified branch (to delete branch even if it has changes unmerged to master)
git branch -D <branch-name>
-
git pull
vs.git pull --rebase
git pull
===git fetch
+git merge
git pull --rebase
===git fetch
+git rebase
- Benefits of
git pull -r
(which usesgit rebase
) is that it solves the same problem asgit merge
- Both are designed to integrate changes from one branch into another branch—they just do it in very different ways. Here is an article comparing git merge vs rebase - The major benefit of rebasing is that you get a much cleaner project history. First, it eliminates the unnecessary merge commits required by git merge. Second rebasing also results in a perfectly linear project history
— you can follow the tip of feature all the way to the beginning of the project without any forks.
git config
- to see all current configuration:
git config -l
- Use search engines
git [subcommand] --help
- Examples:
git --help
git status --help
git commit --help
- Examples:
- Git documentation
- Removing files from git (i.e. gitignoring files that have been committed)
- Explain what these terms mean
repository
master
branchremote
origin
-commit sha - example: 8a295525f6c75325605bf4073c74e6afe02ad43c
- Learn branching
- Remove Sensitive Data from a Repository
- Recommended Git from the bottom up
- Recommended Git from the bottom up (PDF version)
- Mastering Git
- Git documentation
- Learn Git Branching
- Trunk based development > Feature branches
- Git rebase
- git rebase in depth
- Git cheatsheet
- Git Stash
- Another Git Stash Tutorial
- Git Flight Rules. A guide for programmers about what to do when things go wrong.
- Git Katas
- Git in a nutshell
- Git for computer scientists
- Git for age 4 and up