Showing posts with label Git Checkout. Show all posts
Showing posts with label Git Checkout. Show all posts

Saturday, December 20, 2014

Checkout (Create new Branch)

Checkout option in Git is used to switch between an existing branch to another, it can also be used to create a new branch and switch to the newly created branch. In the post Checkout in SourceTree (switch branches) we saw on how to use Checkout to switch to an existing branch, here we shall use the Checkout command to create a new branch and switch to the new branch.

To create a new branch and checkout to the new branch follow the below steps.
1.    Open source Tree.
2. In the repository which I use, we have 2 branches; we shall create a new branch using the Checkout command.


3.    Click on the Checkout option from the Toolbar.
4. Select the Tab “Checkout new branch”, select “master” as the source branch, give a name to the new branch “GitRebase” and click Ok.


5.    Notice that the new branch is created, and is marked as the current branch. Any changes committed at this point will be added to this branch.


6. Let us push the changes so that the new branch is added to the remote repository as well.


7.    Once pushed, you can login into your remote BitBucket repository and notice that the number of branches in the repository has increased by 1.
8.    Now let us add a new file to the branch, I am adding a new file called ‘Git Rebase.docx’
9. Stage the new file, commit and push the changes to the remote repository.


10. Now if we Checkout to the master branch,  you can see that the new file “GitRebase.docx” is not visible, also you can see that the new file is removed from the local working folder, since we added this file to the new Branch.  

Tuesday, December 16, 2014

Checkout in SourceTree (switch branches)

In the previous post Creating a new Branch in SourceTree we created a new branch “GitCommands”, we now have 2 branches “master” and “GitCommands” and the [Head] is pointing to the “GitCommands” branch, we will switch from this branch to the “master” branch.

1.    Open source Tree.

2.    Before switching the branch, check the branch to with the [head] is pointing, now we are in the GitCommands branch.


3.    Click on the Checkout option from the Toolbar.


4.    In the pop-up, select the master branch and click OK.


5.    The [head] now points to the [master] branch, the files in the local working folder are updated to reflect the files in the [master] branch.



Notice that the file “Git Commands.docs” added to the “Git Commands” is not available in the local Working Folder, this is because when you switch to a different branch, the local working folder is refreshed with the files in that branch.

6.    Only one copy of the working folder is maintained, and the files in the working folder reflects the files in the branch to which the head is pointing, hence before switching from one branch to another make sure that the changes are properly committed.
7.  You can also Checkout to an existing branch by right clicking on the branch and selecting the “checkout” option.


Branch Vs Checkout in Git

Branch and Checkout do the same thing but there is a small difference between the two. This post explains the difference between the operation of the branch and checkout commands in Git.

Sunday, December 7, 2014

Checkout a branch using Git Bash

The checkout command is used to switch to a specific branch, also the checkout command gets the latest changes from the specified branch and updates them to the local working folder. Before using the checkout command to switch to a branch, the specific branch should be created using the git branch command.  

The checkout command can be used with the –b switch to create a new branch and switch to that branch in a single command.