Git initial setup

1git config --global user.name "username"
2git config --global user.email "email address"
3git config --global pull.ff only
4git branch -M main

If you forgot to add a file to .gitignore

Sometimes certain file(s) are missed from .gitignore and get committed to the repo. Subsequently adding them to .gitignore does not work because .gitignone will prevent untracked files from being added (without an add -f) to the set of files tracked by Git. However, Git will continue to track any files that are already being tracked.

  • To stop tracking the files, remove them from the index
    • git rm --cached file . Or, to remove a folder and all files in the folder recursively:
    • git rm -r --cached <folder>
  • Then git add .
  • Then commit the change git commit -m "Removed <file> from repo"

Error: You have divergent branches and need to specify how to reconcile them.

A good explanation of this error and how to resolve it is here .

Basically, what has happened is that the remote branch has a commit that was not pulled into the local branch, but the local branch has its own commit that we are trying to push to the remote branch.

The options to fix this are

  1. Create a merge commit: git pull --no-ff. This creates a new commit which is parent to both the new local commit and the new remote commit. Note that merge conflicts are possible and would need to be resolved
  2. Rebase the local commit: git pull --rebase. This temporarily “ignores” then new local commit and pulls in the new remote commit. Then it tries to apply the new local commit on top of the newly pulled remote commit. This results in the creation of a new commit hash (compared to the original new local commit). It may also result in merge conflicts that need to be resolved.
  3. Do nothing and throw an error message if a pure fast-forward commit isn’t possible. User then resolves issues manually.

To force your forked repo to match upstream’s master

1git remote add upstream https://github.com/some_user/some_repo
2git fetch upstream
3git checkout master
4git reset --hard upstream/master  
5git push origin master --force

Source: https://gist.github.com/glennblock/1974465

For syncing tags

1git fetch --tags upstream
2git push -f --tags origin master