Tutorial Level: Medium
This post is intended for readers who already have experience with a version control system.
For official documentation, please reference:
What is git?
Well chap, git is arguably the most popular, modern source code management (SCM) tool in use today. You control it with the command line. When software developers want to collaborate on a project together, the last thing they want to do is worry about stepping on each other’s toes. Git allows developers to write code together without losing changes and revert back from any unwanted changes. With git, you can easily track your project’s codebase, keep it up-to-date, and control the workflow. Lastly, git is extremely light-weight and best of all, it’s open-source.
Need a clean git setup?
Lesson 1: Clean, Initialize, and Branch a git repository.
Pro tip: Don’t nest instances of git.
If you have a messy git implementation and need to start over, here’s how to recursively remove all git-repositories from a directory. Note: This will Not delete your project files.
Open your terminal and navigate to troubled directory.
Check to see if there is a ‘.git’ repository file.
Remove all git related files (force recursive).
rm -rf .git*
See repo status
See all branches
git branch -a
git branch <branch>
Checkout branch (switch to branch)
git checkout <branch>
Push branch and set the remote as upstream branch
git push --set-upstream origin <branch>
Lesson 2: Pull, Commit, Push and Merge.
Update your current branch
Check status of files
Add files to ‘staging’ to be committed
git add <file>
Commit all staged files
git commit -m "<message>"
Stage and commit all files in directory
git commit -am "<message>"
Pro tip: Always pull right before you push to avoid overriding someone else’s recent push.
Push your committed files
Combine specified branch into current branch
git merge <branch>
Lesson 3: Guidelines & Branch Design
Pull before you push.
Leave brief, meaningful messages when you commit. When you need to find a specific change, ambiguous messages will not help you or your team.
Frequent smaller commits tend to be easier to manage than infrequent large commits.
Don’t assign users to work on the same file. This will minimize overriding and merge conflicts.
Use multiple branches: master (remote), develop (remote), and ‘your initials’ (local).
Work on local branch. Teammate’s all push to develop. Team Lead merges develop to master.
Master branch is your production branch. Always keep it in a working state. Always merge to master. Pushing is prone to more errors, so only push when you absolutely have to – such as a hotfix.