Notes from Practical Git for Everyday Professional Use on egghed.io.
Initialize the current local directoy as a git repository locally.
git init --bare will make the local directory a bare repository which will act as a remote respository.
Clone a remote repository locally. New directory will be created.
fileAdd changes infileto index (staging area)..Add changes in current directory to index (staging area).-AAdd all changes locally to index (staging area).
git diff [files]
Difference between working directory and index (staging area). This shows difference that has not been staged (i.e., git add is needed).
git diff --cached [files]
Difference between index (staging area) and last commit/HEAD.
git diff HEAD [files]
Difference between working directory and last commit/HEAD. This shows all changes since last commit, whether they are staged or not.
git diff origin/master [files]
Differences between local and remote.
git commitwithout providing file names
Commits changes in staging to local repository. If a file has changes that are not staged yet, unstaged changes will not be committed.
git commit <filename>
Will commit the file with all changes to local repository. If a file has changes that are not staged yet, unstaged changes will be committed too.
Push changes to remote.
Pulls changes from remote. If conflicts happen, manually update the file(s) and commit.
git log --oneline --graphgit log --after="yesterday" --before="10 minutes ago"git log -i <...>git log --author="name1\|name2"git log --grep="name"git log -p -S"<search-string-in-codes>"git log -p -Gcode1\|code2git log --no-mergesgit log -3git log master..hot-fixgit log file1 file2
Checks out the branch and work in the branch.
git branch -v shows branches with latest commit.
git branch <name> creates a new branch.
git checkout -b <name> creates a new branch and checks it out.
git branch -d <name> removed the branch.
git merge <branch> meargs changes in the branch into current branch and create a merge commit.
If conflicts happen, manually update the file(s) and commit.
git stash stashes changes in the working directory (which cleans up the working directory) so that we can work on a hot fix.
git stash apply brings back stashed changes.
Lists all changes to the file in chronological order. Commit id, author, commit time and changed lines will be displayed.
Sematic versioning: major.minor.patch
git tag to list all tags
git tag <tag-name> to add a tag
git tag -a <tag-name> -m "tag message" to add a tag message
git push origin <tag-name> to push a tag to a remote repository
Exmaple: git rebase -i origin/master
Clean up commits (roll up a few commits to one) locally before pushing to remote. This destroys commit histories.
Find out where a bug is introduced.
git bisect bad marks the current commit as bad.
git bisect good <commit-id> does a binary search between the good and the bad commits and automatically checks out a commit between the two. Test now to see if the new current commit is bad or good.
Do the above iteratively until you find the codes are working.
git bisect reset then resets the git status.
Add hooks
git config --global alias.graph 'log --graph --oneline'git config --listgit config --list --systemgit config --list --globalgit config --list --localgit config --edit --systemgit config --edit --globalgit config --edit --local
Remove git tracking
git rm --cached <file> to remove a file that is added to git before being added to .gitignore from git tracking. --cached means the file will be left in working tree.