Skip to Content
Version Control with Git
book

Version Control with Git

by Jon Loeliger
June 2009
Intermediate to advanced
330 pages
9h 29m
English
O'Reilly Media, Inc.
Content preview from Version Control with Git

Using git mv

Suppose you need to move or rename a file. You may use a combination of git rm on the old file and git add on the new file, or you may use git mv directly. Given a repository with a file named stuff that you want to rename newstuff, the following sequences of commands are equivalent Git operations:

$ mv stuff newstuff
$ git rm stuff
$ git add newstuff

and

$ git mv stuff newstuff

In both cases, Git removes the pathname stuff from the index, adds the new pathname newstuff, keeps the original content for stuff in the object store, and reassociates that content with the pathname newstuff.

With data back in the example repository, let’s rename it and commit the change:

$ git mv data mydata

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       renamed:    data -> mydata
#

$ git commit -m"Moved data to mydata"
Created commit ec7d888: Moved data to mydata
 1 files changed, 0 insertions(+), 0 deletions(-)
 rename data => mydata (100%)

If you happen to check the history of the file, you may be a bit disturbed to see that Git has apparently lost the history of the original data file and remembers only that it renamed data to the current name:

$ git log mydata
commit ec7d888b6492370a8ef43f56162a2a4686aea3b4
Author: Jon Loeliger <jdl@example.com>
Date:   Sun Nov 2 19:01:20 2008 -0600

    Moved data to mydata

Git does still remember the whole history, but the display is limited to the particular filename you specified in the command. The --follow ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Version Control with Git, 2nd Edition

Version Control with Git, 2nd Edition

Jon Loeliger, Matthew McCullough
Version Control with Git, 3rd Edition

Version Control with Git, 3rd Edition

Prem Kumar Ponuthorai, Jon Loeliger
Git for Programmers

Git for Programmers

Jesse Liberty

Publisher Resources

ISBN: 9780596158187Errata