Solutions for Git Workflow Problems

I want to rename a branch
$ git branch -m <currentname> <newname>
Source: blog

I’ve forgotten which branch my branch was created from
$ git log -g <branchname>
$ git reflog show <branchname>
and look for the first entry (assumes branch was created recently).
Source: Stack Overflow

Careful! The following hints assume you haven’t pushed your changes.

I’ve just committed my changes to the wrong branch
1. Do $ git reset --soft HEAD^ – this will undo the commit but leave the changes staged.
2. Checkout the branch you actually wanted to commit to.
3. Commit your changes.
Source: Stack Overflow

I’ve just done a hard reset and need to undo it
1. Do $ git reflog and look up the sha1 of the commit you want to go back to
2. Do $ git reset --hard <sha1 to return to>
Source: Stack Overflow

I missed out a file a couple of commits ago
1. Commit the files you want to add to the commit
2. Do $ git rebase -i HEAD~<number of commits to go back>
3. Your text editor will bring up the list of commits. Re-arrange the rows into the desired order (oldest first) and change ‘pick’ to ‘squash’ for the commit you want to merge.
4. After saving that file, the text editor will ask you to specify a message for the merged commit.
Source: git-scm

About Jennifer Phillips Campbell

Software Developer and Medieval Historian
This entry was posted in Version Control. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s