Skip to content

Instantly share code, notes, and snippets.

@natersoz
Last active October 20, 2023 16:57
Show Gist options
  • Save natersoz/35043765446fa8b818b05402e20266f7 to your computer and use it in GitHub Desktop.
Save natersoz/35043765446fa8b818b05402e20266f7 to your computer and use it in GitHub Desktop.
git usage notes

Git Usage Notes

Stuff that I'm always having to look up.

git rebase

When working in your branch and you want to bring it up to date with main: :

$ git rebase -s recursive -X ours main      # Resolves conflicts in favor of main.
$ git rebase -s recursive -X theirs main    # Resolves conflicts in favor of your local branch.

When in the middle of a rebase and you want to resolve a conflict use the checkout command. :

$ git checkout --ours path/filename # Resolves conflicts in favor of the branch being placed into your local branch.
                    # In other words, if you were performing 'git rebase main' the conflict is resolved in favor of main.
$ git checkout --theirs path/filename   # Resolves conflicts in favor of your local branch prior to rebasing.

git merge and git pull

$ git merge -s recursive -X ours <branch-name>      # Resolves conflicts in favor of local branch copy.
$ git merge -s recursive -X theirs <branch-name>    # Resolves conflicts in favor of <branch-name>.
$ git pull  -s recursive -X ours origin <branch-name>   # Resolves conflicts in favor of local branch copy.
$ git pull  -s recursive -X theirs origin <branch_name> # Resolves conflicts in favor of origin/<branch_name>

When in the middle of a merge/pull and you want to resolve a conflict use the checkout command. :

$ git checkout --ours path/filename # Resolves conflicts in favor of the local copy
$ git checkout --theirs path/filename   # Resolves conflicts in favor of the branch being merged in to your local copy.

git commit splitting

* Start an interactive rebase with :

$ git rebase -i <commit>^

Where <commit> is the commit to change (or the last commit in a list to change), and ^ indicates that the rebase will start one before the <commit>.

  • Mark the commit(s) you want to split with edit.

* Start editing that commit: :

$ git reset HEAD^   # The HEAD and the index are rewound by one.
  • git add the changes to the index that you want to have in the first commit.
  • git commit the changes to the index.
  • Repeat the last two steps until your working tree is clean.

* Continue the rebase with :

$ git rebase --continue

git clean and untracked files

$ git clean -d                  # Remove untracked directories
$ git clean -x                  # Remove ignored files
$ git clean -f                  # Don't ask stupid questions, do it!
$ git clean --dry-run           # Tell me what will happen, but don't actually delete anything.
$ git clean -i --interactive    # Interactive mode.
$ git ls-files --others         # List files not under git version control.

git pulling changes from local repositories

In this example add the repository ~/repo2 as a 'remote' in the repository cloned into ~/repo1 : :

$ cd ~/repo1                # Working from within the scope of repository 1
$ git remote add repo2 ~/repo2      # Add the cloned repository in ~/repo2 as a remote for the repository in ~/repo1
$ git fetch repo2           # Update the remote repo2 into repo1
$ git merge repo2/foo           # Operations can be performed using the repo2 repository

git cat

There is no git cat, but there should be. Figuring out how to do this is horrific. Just do this: :: $ git show <HASH>:path/file $ git show <branch>:path/file

Note that when performing this command the file specified might be in your local directory. Git will insist you use the relative path ./ prefix before the file name. The git command line guidance is very helpful here.

git log, ecluding paths

Full description in Stack Overflow :: $ git log -- . ':(exclude)sub/sub/file'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment