In this part of my Git video tutorial I cover Branching, HEAD, Checkout, Rebasing, Reverting, Reset, Clean, and Mergetool. I also spend some time explaining what happens when you commit and how to solve merge conflicts.
If you haven’t watched parts 1 and 2 of this tutorial you definitely should watch them first so that this isn’t confusing. I also provide a cheat sheet / transcript below after the video to aid in translation.
If you enjoy videos like this, it would help my Google search ranking if you shared with a click here
Other Git Video Tutorial Videos
Init, Add, Commit, Status, Diff, Rm, Log and Installation : http://goo.gl/L3bR9f
GitHub, Remote Repositories, Push, Pull, Remote, Fetch, Tagging, Aliases and Clone : http://goo.gl/ctIF44
Branching, HEAD, Checkout, Rebasing, Reverting, Reset, Clean, Solving Merge Conflicts and Mergetool : http://goo.gl/zWDj7T
Generating SSH keys, how to set up multiple GitHub accounts on one computer and the Fork & Pull workflow : http://goo.gl/jQo7rv
Git Video Tutorial Cheat Sheet / Transcript
---------------- GIT PART 3 ---------------- 1. Branching allows you to take a project in your own direction without effecting the main code. You use them to make sure you don't introduce unstable code to the master branch. 2. When you commit a project : a. Each file is given a hash code b. A tree object that contains those files and the associated hash codes receives a hash code c. A commit object stores a reference to the tree and other data like the author, commit comment, a reference to the previous commit and other data. 3. The default branch is called the master. As you make additional commits the current newest version is referred to as the master. 4. If you create a branch you can go in a different direction with the project without effecting the master until you merge. Each branch creates a new pointer to a committed version of files and doesn't make another copy of the files. 5. We can actually create many branches, but be careful while doing this because it can get to be hard to merge multiple branches. A pointer known as HEAD can be pointed at any committed version or to any branch with the checkout command. 6. When you are finished with your branch you can merge back into the master commit and move on. You can also do all of this locally or on a remote. 7. ---------------- Simple Branch Example ---------------- a. git checkout -b fix20 # Create a branch and switch to it I. Same as : git branch fix20 git checkout fix20 b. Change AndroidManifest.xml in vim c. git commit -a -m 'Added Branch fix20' # Commit the change to the branch, but not to master d. git checkout master # Switch to master e. git push origin fix20 # Push the branch to GitHub f. git fetch origin # If someone else fetches from the server they get a reference to the branch on the server but not all the files g. git checkout -b fix20 origin/fix20 # Retrieves the branch fix20 h. git branch # Shows all branches i. git branch --merged # Shows all merged branches j. git branch --no-merged # Shows unmerged branches k. git branch -v # Shows all branches and their last commits # * Points out the branch currently checked out l. git merge fix20 # Merge the branch version with the master git push # Push the change to GitHub m. git branch -d fix20 # You can delete merged branches with this n. git branch -D fix22 # Deletes unmerged branches o. git push origin :fix20 # Deletes the branch on GitHub p. git branch -m newBranchName # Renames a branch 8. ---------------- Multiple Branch Example ---------------- a. a. git checkout -b fix21 # Create a branch and switch to it b. Edit AndroidManifest.xml c. git commit -a -m 'Added Branch fix21' d. git checkout master # Switch to master e. Look at Manifest to see that nothing changed f. git checkout -b 'hotfix' # Create a new branch g. git commit -a -m 'Added Hot Fix' # Commit the Hot Fix h. git checkout master # Switch to master i. git merge hotfix # Merge the hotfix version with the master j. git branch -d hotfix # Delete the hotfix branch k. git checkout fix21 # Switch to fix21 branch l. git checkout master # Make sure you are in master m. git merge fix21 # Merges the branch and master if there are no conflicts n. If there is a conflict resolve it o. git branch -d fix21 # Delete the unneeded branch git branch -D fix21 # To force delete p. git mergetool # You can merge with a graphical tool I. Backup : Contents of the file before calling the merge tool II. Base : The common ancestor of the files being merged III. Local : Version being pointed at by HEAD IV. Remote : The branch being merged into head 9. ---------------- Rebasing Example ---------------- a. Rebasing moves a branch to a new ( master / base ) commit. This is also referred to as a fast forward merge. Just never rebase commits that have been pushed to a public repository b. git checkout -b fix22 c. Edit AndroidManifest.xml d. git commit -a -m 'Changed the comment to 10' e. git checkout -b hotfix f. Edit another file other then AndroidManifest g. git commit -a -m 'Edited file....' h. git checkout master i. git merge hotfix j. git branch -d hotfix k. git checkout fix22 l. git rebase master # Move branch to new master commit m. git checkout master n. git merge fix22 10. ---------------- Reverting Vs. Resetting Example ---------------- a. Some times you want to eliminate a previous commit, but you still want to keep the commit for integrity reasons. Revert undoes changes made in that commit and makes a new commit. Reset actually deletes the commit which can cause problems. b. Do something that will be undone c. git commit -m 'Made a change that I will undo' d. git revert HEAD # You are back to where you started, but the commit was made e. Reset eliminates previous commits and you can never get them back. You really should never use it actually. f. git reset someFile # Removes a file from the staging area, but leave the working directory unchanged g. git reset # Reset the staging area to match the most recent commit while leaving the working directory unchanged h. git reset aCommit # Move back to this previous commit, reseting the staging area, but not the working directory i. git reset --hard # Reset both the staging area and working directory to match the most recent commit j. git reset --hard aCommit # Move back to the commit listed and change staging and working directory 11. ---------------- Clean Example ---------------- a. Clean removes untracked files from your directory and is undoable. b. git clean -n # Shows which files will be removed c. git clean -f # Remove untracked files d. gir clean -df # Remove untracked files and untracked directories in the current directory e. git reset --hard # Undoes changes on all tracked files git clean -df # Removes all untracked files