Git Patching Questions


Questions, Explanations, Howtos

Temple Termagant

Posts: 6

Joined: Tuesday, 3rd December 2013, 21:56

Post Saturday, 7th December 2013, 00:13

Git Patching Questions

Hello,
I'm fairly new to git so I have a few questions regarding patches.

I have been experimenting with the crawl code by adding in a new species, and after a bit of trial and error, committed some additions and made a patch out of them. However, I found a few whitespace errors my code and while they were simple to fix, I am having trouble applying these fixes to my patch.

The best I can do is commit the fixes and make a new patch out of them, which gives me two patches, one with the changes, and one with the fix to the changes. How do I just 'update' my first patch to have the fixes?


Another question: How would I go about 'uncommitting' my changes to the code? I would like to test if applying the patch to a fresh install would work. I could clone the crawl code again, but is there a better way?

Swamp Slogger

Posts: 131

Joined: Saturday, 2nd November 2013, 08:39

Location: Mother Russia

Post Saturday, 7th December 2013, 05:51

Re: Git Patching Questions

You should use
  Code:
 $ git commit amend

it will merge new changes with your last commit.

Spider Stomper

Posts: 243

Joined: Sunday, 28th August 2011, 14:04

Post Saturday, 7th December 2013, 14:45

Re: Git Patching Questions

You should learn how to use branches. They will solve all your problems.

Branches are like different versions or variants of a project, but only one of them can be active at the same time (e.g. the current files in your hd belong to that branch). Each time you commit some changes to a project, they are actually applied only to the current branch.

To see the list of local branches, and which one is the current one:
  Code:
$ git branch


To create a new branch:
  Code:
$ git branch branch-name


To switch to another branch:
  Code:
$ git checkout branch-name


Thanks to branches you won't need to 'uncommit' anything, and creating patches will be easier. Make sure you never commit changes to the master branch. That one is linked to a remote repository. Commit them to a new branch, and then:
  Code:
$ git format-patch master

For this message the author CommanderC has received thanks:
rchandra

Temple Termagant

Posts: 6

Joined: Tuesday, 3rd December 2013, 21:56

Post Saturday, 7th December 2013, 20:13

Re: Git Patching Questions

Thanks for the tips. It looks like that will solve all my problems.
User avatar

Dungeon Master

Posts: 4031

Joined: Thursday, 16th December 2010, 20:37

Location: France

Post Monday, 9th December 2013, 10:40

Re: Git Patching Questions

To update your latest commit, add the new changes (git add foo.cc), then amend the commit (git commit --amend).
To merge the last 2 commits together, use interactive rebase: git rebase -i HEAD~2. Then, change the second commit to squash or fixup.
<+Grunt> You dereference an invalid pointer! Ouch! That really hurt! The game dies...
User avatar

Spider Stomper

Posts: 186

Joined: Friday, 8th March 2013, 13:27

Post Tuesday, 10th December 2013, 18:07

Re: Git Patching Questions

I'm also learning how to use git.
If I understood correctly what happens in a branch stays in a branch.
Still I'm having touble with that, am I doing something wrong?

  Code:
echo "Example crawl code" > crawl_file
git add crawl_file
git branch new_branch
git checkout new_branch
echo "New code line" >> crawl_file
git checkout master

#Here I'm in the master.
#Why do I see changes made in new_branch?
cat crawl_file
>Example crawl code
>New code line


Edit: added line "git add crawl_file"
Last edited by Marbit on Tuesday, 10th December 2013, 18:25, edited 1 time in total.

Halls Hopper

Posts: 69

Joined: Monday, 20th February 2012, 10:43

Post Tuesday, 10th December 2013, 18:14

Re: Git Patching Questions

You must commit your changes before changing branch.

Once the commit is done, it becomes part of the branch. And all non commited changes are transfered to the new branch when you switch via checkout.
User avatar

Spider Stomper

Posts: 186

Joined: Friday, 8th March 2013, 13:27

Post Tuesday, 10th December 2013, 19:02

Re: Git Patching Questions

As a follow up I commited a change... but then when trying to switch to the master branch it seems I didn't. :shock:

  Code:
echo "Example crawl code" > crawl_file
git add crawl_file
git branch new_branch
git checkout new_branch
echo "New code line" >> crawl_file
#Let's commit the changes
git commit -m "hopefully changes won't be in master branch"
git checkout master

>error: Your local changes to the following files would be overwritten by checkout:
>crawl-ref/source/crawl_file
>Please, commit your changes or stash them before you can switch branch
>Aborting

#WTF! I thought that I had already commited my changes!

Halls Hopper

Posts: 69

Joined: Monday, 20th February 2012, 10:43

Post Tuesday, 10th December 2013, 19:21

Re: Git Patching Questions

My bad.

You must add the files you made changes on and then commit it. I'm a user of the short instruction (git commit -a) but manualy specifing wich files you want to add to your commit is safer.

Dungeon Dilettante

Posts: 1

Joined: Wednesday, 5th December 2012, 15:44

Post Thursday, 12th December 2013, 20:33

Re: Git Patching Questions

Marbit wrote:As a follow up I commited a change... but then when trying to switch to the master branch it seems I didn't. :shock:



If you're still in new_branch right now, run
  Code:
git diff

You should see the addition of "New code line" in the diff output. What happened is that when you ran "git add crawl_file", you told it to commit the version of the file present at the time you ran that command. You then committed that version in the "new_branch" branch.

However, your addition of "New code line" was never added or committed. Checking out master would restore a version of your local directory that does not include crawl_file at all, much less "New code line". You'll have to run
  Code:
git add crawl_file
again and then
  Code:
git commit
for that new line to be saved.

Generally, you only want to run "git add [filename]" when the file is exactly how you want it to be committed. If you run it multiple times on the same file before committing, it will commit the last version of the file you give it.

Return to Coding

Who is online

Users browsing this forum: No registered users and 7 guests

cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.