Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a file to the first commit on a git repo?

Tags:

git

rebase

I have been converting old SVN repos to GIT. I have gotten the branches and tags to convert. Including making SVN tags real tags in GIT.

However, I would like to add a gitignore to the very first commit in the newly created GIT repo. I want it to be as if the file had always been there. So all the commits that followed (on master or in branches) would now have this file as the parent tree(s) would lead back the the first commit.

It seems that some form of git rebase or git filter-branch --tree-filter is what I need. I have tried each of these but I end up with disconnected branches.

like image 992
Carl Stewart Avatar asked Sep 03 '11 19:09

Carl Stewart


People also ask

How do you go to the first commit of a repo?

Usage. Go to any particular repo's landing page (e.g. like the one you're on) and click the bookmarklet, which will take you to the first page (initial commit). By default, it tracks the master branch, but if you change the branch on the landing page, it will go to that branch's first commit.

How do you add to a specific commit?

Use git commit --amend to change your latest log message. Use git commit --amend to make modifications to the most recent commit. Use git rebase to combine commits and modify history of a branch.

How do I add a file to GitHub repository terminal?

On your computer, move the file you'd like to upload to GitHub into the local directory that was created when you cloned the repository. Open Terminal . Change the current working directory to your local repository. Stage the file for commit to your local repository.


2 Answers

In case anyone needs to do this in the future, here's an easier solution. I recently had to modify an early commit because of a problematic typo in the commit message. This works for changing anything about a previous commit. I modified Charles Bailey's answer here.

# Go back to the commit you want to change (detach HEAD)
git checkout <sha1_for_commit_to_change>

# Make any changes now (add your new file) then add them to the index
git add <new_files>

# amend the current commit
git commit --amend

# temporarily tag this new commit
# (or you could remember the new commit sha1 manually)
git tag tmp

# go back to the original branch (assume master for this example)
git checkout master

# Replay all the commits after the change onto the new initial commit
git rebase --preserve-merges --onto tmp <sha1_for_commit_to_change>

# remove the temporary tag
git tag -d tmp

Note that this will change all of your commit IDs following the amended commit, so it's not recommended on a public repository. Also, you'll have to recreate all of your tags

like image 62
Mason Heller Avatar answered Sep 30 '22 04:09

Mason Heller


Do this on a clean working tree, on a new clone if you wish:

$ git checkout -b withgitignore $firstcommithash
$ git add .gitignore
$ git commit --amend
$ for branch in branch1 branch2 branch3 ... ; do
      git checkout $branch
      git rebase withgitignore
   done

Untested ;)

like image 37
holygeek Avatar answered Sep 30 '22 02:09

holygeek