Archive for category GIT

Git rename branch

Rename a local and remote branch in git

If you have named a branch incorrectly AND pushed this to the remote repository follow these steps before any other developers get a chance to jump on you and give you shit for not correctly following naming conventions.

1. Rename your local branch.
If you are on the branch you want to rename:

1
git branch -m new-name

If you are on a different branch:

1
git branch -m old-name new-name

2. Delete the old-name remote branch and push the new-name local branch.

1
git push origin :old-name new-name

3. Reset the upstream branch for the new-name local branch.
Switch to the branch and then:

1
git push origin -u new-name

Leave a comment

Git History

Goals

  • To learn to view the project’s history.

Getting a list of changes made is a function of the git log command.

RUN:

git log

You will see …

RESULT:

$ git log
commit fa3c1411aa09441695a9e645d4371e8d749da1dc
Author: Alexander Shvets <alex@githowto.com>
Date:   Wed Mar 9 10:27:54 2011 -0500

    Added HTML header

commit 8c3228730ed03116815a5cc682e8105e7d981928
Author: Alexander Shvets <alex@githowto.com>
Date:   Wed Mar 9 10:27:54 2011 -0500

    Added standard HTML page tags

commit 43628f779cb333dd30d78186499f93638107f70b
Author: Alexander Shvets <alex@githowto.com>
Date:   Wed Mar 9 10:27:54 2011 -0500

    Added h1 tag

commit 911e8c91caeab8d30ad16d56746cbd6eef72dc4c
Author: Alexander Shvets <alex@githowto.com>
Date:   Wed Mar 9 10:27:54 2011 -0500

    First Commit

Here is a list of all the four commits to the repository, which we were able to make so far.

01One line history

You fully control over what the log shows. I like the single line format:

RUN:

git log --pretty=oneline

You will see …

RESULT:

$ git log --pretty=oneline
fa3c1411aa09441695a9e645d4371e8d749da1dc Added HTML header
8c3228730ed03116815a5cc682e8105e7d981928 Added standard HTML page tags
43628f779cb333dd30d78186499f93638107f70b Added h1 tag
911e8c91caeab8d30ad16d56746cbd6eef72dc4c First Commit

02Controlling the display of entries

There are many options to choose which entreis appear in the log. Play around with the following parameters:

git log --pretty=oneline --max-count=2
git log --pretty=oneline --since='5 minutes ago'
git log --pretty=oneline --until='5 minutes ago'
git log --pretty=oneline --author=<your name>
git log --pretty=oneline --all
git log -- [filename]

Leave a comment

Discarding local changes (before staging)

Goals

  • To learn how to discard the working directory changes

01Checking out the Master branch

Make sure you are on the lastest commit in the master brach before you continue.

RUN:

git checkout master

02Change hello.html

It happens that you modify a file in your local working directory and sometimes wish just to discard the committed changes. Here is when the checkout command will help you.

Make changes to the hello.html file in the form of an unwanted comment.

FILE: HELLO.HTML

<html>
  <head>
  </head>
  <body>
    <h1>Hello, World!</h1>
    <!-- This is a bad comment.  We want to revert it. -->
  </body>
</html>

03Check the status

First of all, check the working directory’s status.

RUN:

git status

RESULT:

$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   hello.html
#
no changes added to commit (use "git add" and/or "git commit -a")

We see that the hello.html file has been modified, but not staged yet.

04Undoing the changes in the working directory

Use the checkout command in order to checkout the repository’s version of the hello.html file.

RUN:

git checkout hello.html
git status
cat hello.html

RESULT:

$ git checkout hello.html
$ git status
# On branch master
nothing to commit (working directory clean)
$ cat hello.html
<html>
  <head>
  </head>
  <body>
    <h1>Hello, World!</h1>
  </body>
</html>

The status command shows there were no unstaged changes in the working directory. And the “bad comment” is no longer contained in the file.

Leave a comment