Git

Git Common Issues

https://about.gitlab.com/blog/2018/08/08/git-happens/

File that you changed, but you really shouldn’t have

https://stackoverflow.com/questions/3334475/git-how-to-update-checkout-a-single-file-from-remote-origin-master

git checkout origin/master -- path/to/file

Forgot to add a file / Commit Message Typo

Use --amend flag with git commit. Add that missed file then run that trusty command.

git add missed-file.txt
git commit --amend

Manage multiple commit

Use ainteractive rebase in git. Allows you to:

  • re-order your commits
    • merge multiple commits into one
    • remove commits
    • split a single commit into multiple commits
    • change a commit’s contents or commit message
    • perform any combination of the above

Take a look at this tutorial.

One very common operation to perform is to make changes to a commit buried in history. This is so common that git has dedicated support for this. In order to do this, first create a fixup commit aimed at the commit your are trying to modify. Make your changes on top of all of your commits, and then do

git add file1 file2 ...
git commit --fixup <id of commit you want to change>

This will create a temporary fixup commit. In order to merge this commit with the one you are trying to modify, do

git rebase -i --autosquash <id of commit you want to change>~

Don't forget the ~!

There is a tilda ~ after the commit id to select the commit before the one you are trying to modify. See this detailed example for clarification.

6.9.6. dazel configuration

I added a file I didn’t want in the repo

If all you did was stage the file and you haven’t committed it yet, it’s as simple as resetting that staged file:

git reset /assets/img/misty-and-pepper.jpg

If you’ve gone as far as committing that change, you need to run an extra step before:

git reset --soft HEAD~1
git reset /assets/img/misty-and-pepper.jpg
rm /assets/img/misty-and-pepper.jpg
git commit

This will undo the commit, remove the image, then add a new commit in its place.

From CS349

§ If you want to ignore a file that was tracked by accident

git rm --cached [filename]
git add -all
git commit -m "removed files tracked by mistake"

§ If you want to ignore all files that were tracked by accident:

git rm --cached -r [directory]
git add -all
git commit -m "cleaning files that should have been ignored"

Oops… I committed all those changes to the master branch

So you’re working on a new feature and in your haste, you forgot to open a new branch for it. You’ve already committed a load of files and now them commits are all sitting on the master branch. Luckily, GitLab can prevent you from pushing directly to master. So we can roll back all these changes to a new branch with the following three commands:

Note

Make sure you commit or stash your changes first, or all will be lost!

git branch future-brunch
git reset HEAD~ --hard
git checkout future-brunch

This creates a new branch, then rolls back the master branch to where it was before you made changes, before finally checking out your new branch with all your previous changes intact.

Branch Spelling Mistake

We rename this branch in a similar way to how we rename a file with the mv command: by moving it to a new location with the correct name.

git branch -m future-brunch feature-branch

If you’ve already pushed this branch, there are a couple of extra steps required. We need to delete the old branch from the remote and push up the new one:

git push origin --delete future-brunch
git push origin feature-branch

Made a commit on the wrong branch

git reset HEAD~1
  • This will “uncommit” your commit

https://www.eficode.com/blog/what-to-do-when-you-commit-to-the-wrong-git-branch