Git Part 2 Add Commit Branch Delete Revert


This entry is part 2 of 5 in the series Git and GitHub

This is part two of our Git series of posts. The previous post was called Git Introduction.

The next thing to work on is branching, but before doing that we will want to review Commit. Why do we need to commit? If you have several people working on the same project and everything is committed automatically it would be very difficult to revert back to previous code if “bad” code is constantly being automatically committed. We need to manually commit our updates.

YouTube Videos

Here is a video by David Mahler on Git Branching called Introduction to Git – Branching and Merging.

Assumptions

At this point I assume that you have a project you are working on and you have created a Git repository for it. You have added one text file to the repository and committed it. When you execute the git status command, everything is clean. You have made a minor change to the text file that you are happy with and you need to add that to the repository. How do you do that?

Add and Commit

You have made a minor change to one of your files in your project. You have double-checked that the change is good and you want to save it to the repository. You need to add and commit. Assume you are in the correct folder of git bash and you are ready to update to the repository the changes you made to the file called mytext.txt. Issue the following commands.

git add mytext.txt
git commit mytext.txt -m "made an update to mytext"

Major Changes

Minor changes in text is one thing, but changing or even adding new code to a project is more risky and may require a Branch. This keeps our original code safe. We can go off on a branch, make changes, commit them and test the new code. If we made a major mistake we have only affected the branch and not the master. If our branch proves to be in good order, we can then merge that branch into the master and in this way the branch code becomes part of the master.

Branching

To create a branch, all you do is use the branch command like so:

git branch testbranch1

We can see a list of branches by typing the command git branch. We now want to work in our new branch because we wish to make some major changes. When we commit we want to commit to the new branch, not the master branch. Once we are certain our code (or changes) works properly, we can merge our working branch into the master project.

To switch, use checkout.

git checkout testbranch1

You can create a new branch and use checkout all at the same time with code such as this:

git checkout -b <branch_name>

Back to Work

You’ve worked on your project in a branch. Suppose you make changes, but the changes are no good. You save it anyway. You have saved your text file but it is full of bad code. You then go ahead and add and commit using Git.

git add .
git commit -m "looks good (but really it is all messed up)"

We have committed to the testbranch1. Go back to the master branch and open your file and see that you original text is there! Those bad changes you made are gone!

The Branch Process and the Merge command

Normally you create a test branch, work in it, save your work, test your work, add and commit it in the test branch, and once you are satisfied with your work, you can merge the test branch with the master branch and then delete the test branch. So how do we merge branches? Again, at this point we are satisfied with the work we’ve been doing in our test branch (called testbranch1). The first thing to do is switch to the master branch using git checkout master. Now we type git merge testbranch1. Now we can delete the test branch. How do we do that? We’re in the master branch now. Type git branch testbranch1 -d.

Delete a Branch

First, check which branch we are on right now with git branch. The one with the asterisk is the current branch. We need to be sure we are sitting on the master branch to be able to delete another branch. If necessary, enter git checkout master. If necessary, type git merge . To delete a branch, type git branch -d. If you now type git branch you should no longer see your “test” branch listed.

git branch <branch_name> -d

Sometimes deleting a branch will not work. If you have not yet committed changed or you have not merged branches together. You can force a delete with the capital D. Type git branch -D.

Log and Revert

It is possible to go back to a state after you have added and committed your work. If you want to “undo” a commit you can first type git log. This gives you the commit ID.

// make some changes - edit a file, add files etc.
git add .
git commit -m "edited files and added other files etc."
git status // it will say nothing to commit working directory clean
git log
// copy the long ID string of the commit you just made 
git revert <git ID pasted in here>
Ctrl+X
Series Navigation<< Git IntroductionGit Part 3 gitignore >>