This is a list of git notes that I use everyday. Hopefully someone else finds it useful. I will update this list over time.
- The master branch reflects production ready code only. This branch should be deployable at any time and merges to this branch should not break the build process.
- Feature branches should be merged into the master when they are complete.
- Feature branches consist of a single feature or discrete unit of work
- Commit early and commit often!
Generating SSH-Public Key
- Switch to your .ssh directory and check if the id_rsa.pub file exists, this is your public key
- If it doesn’t exist, generate one with the following command
ssh-keygen -t rsa -C "youremail@email.com"
Output is something like the following:
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/danielbeard/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/danielbeard/.ssh/id_rsa.
Your public key has been saved in /Users/danielbeard/.ssh/id_rsa.pub.
The key fingerprint is:
43:c5:5b:5h:b1:f1:51:43:ad:20:a6:92:6a:1f:8a:3a danielbeard@iosdev.local
Copy the contents of the public key file to your clipboard:
pbcopy < ~/.ssh/id_rsa.pub
Normal Workflow
- Checkout the remote repository with:
git clone git@github.com:RepoOwner/RepoName.git
- Updating the repository (won’t need to be done the first time):
git pull
- Create a new branch from the master or current development branch
git checkout -b featurebranchname master
- Make changes to local files
- Check local modifications with
git status
- Stage certain files with
git add <file1> <file2>
etc or add all changesgit add .
- Commit to you local repository
git commit -m 'commit message'
- Push the local commit to the remote repository and set up for tracking
git push -u origin featurebranchname
- Repeat until feature branch is complete.
- Merge master or development branch into the working branch
git checkout master
git pull
git checkout featurebranchname
git merge master
- Push to remote repository
git push -u origin featurebranchname
- Submit pull request and wait for code review
Repository Changes
- Checking which files are in what state:
git status
- Tracking new files:
git add README
- Committing your changes:
git commit -m 'Commit message goes here'
- Push your current master to the remote origin:
git push -u origin master
- Unstaging a staged file:
git reset HEAD <file>
- Stage all untracked files -
git add -u
- Unmodifying a modified file:
git checkout -- benchmarks.rb
Notes:
- The git add command stages a file for a commit. Calling git status shows which files are staged/unstaged or if they are untracked.
- If you modify a file after calling git add, the git status command will show the file as being both staged and unstaged.
- If you modify a file after calling git add, you have to run git add again to stage the latest version of the file Providing the -a option to git commit makes git automatically stage every file before doing the commit. (Letting you skip the git add part)
Basic repository changes: http://git-scm.com/book/en/Git-Basics-Recording-Changes-to-the-Repository
Branching
- Creating a new branch:
git checkout -b branchname
- Branching from a current branch:
git checkout -b feature devbranch
- Pushing branch to remote with tracking:
git push -u origin branchname
- Rename branch
git branch -m <oldname> <newname>
- Delete the local branch:
git branch -d branchname
- Delete the remote branch:
git push origin :branchname
- Listing all branches:
git branch -a
- Just list remote branches:
git branch -r
- Checking out a tracked remote branch:
git checkout --track origin/branch_name
- Merging a branch
- Must have committed all changes (at least locally) like so:
git commit -a -m "Made a change in this branch"
- Switch to whichever branch you are merging back into, e.g. master
git checkout master
- Merge the branch (ALWAYS USE NO-FF):
git merge --no-ff branchname
- Must have committed all changes (at least locally) like so:
Notes:
Tracked branches are local branches that have a direct relationship to a remote branch. If you’re on a tracking branch and type git push or git pull, git automatically knows which branch and server to push/pull from.
Git remote branches :http://git-scm.com/book/en/Git-Branching-Remote-Branches#Tracking-Branches Git local branches: http://git-scm.com/book/en/Git-Branching-Basic-Branching-and-Merging
Tagging
- Only used for tagging master branch merges or “releases”
- You can tag at any point, it doesn’t have to be immediately.
Show the commit history like this: git log --pretty=oneline
15027957951b64cf874c3557a0f3547bd83b3ff6 Merge branch 'experiment'
a6b4c97498bd301d84096da251c98a07c7723e65 beginning write support
0d52aaab4479697da7686c15f77a3d64d9165190 one more thing
6d52a271eda8725415634dd79daabbc4d9b6008e Merge branch 'experiment'
0b7434d86859cc7b8c3d5e1dddfed66ff742fcbc added a commit function
4682c3261057305bdd616e23b64b0857d832627b added a todo file
166ae0c4d3f420721acbb115cc33848dfcc2121a started write support
9fceb02d0ae598e95dc970b74767f19372d61af8 updated rakefile
964f16d36dfccde844893cac5b347e7b3d44abbc commit the todo
8a5cbc430f1a9c3d00faaeffd07798508422908a updated readme
- To tag a certain commit, specify the commit checksum (or part of it) at the end of the command
git tag -a v1.2.1 9fceb02
- Then to transfer all your tags to the remote repository
git push origin --tags
Git tags: http://git-scm.com/book/en/Git-Basics-Tagging
Tracking changes between branches
- Show the modified files between two branches:
git diff --name-status master..branch
- Show side by side comparison :
git difftool -t vimdiff master..branch
Other notes and links
Git aliases: http://git-scm.com/book/en/Git-Basics-Tips-and-Tricks#Git-Aliases Branching model: http://nvie.com/posts/a-successful-git-branching-model/ GitHub flow: http://scottchacon.com/2011/08/31/github-flow.html