Bookmark and Share

Topics

Run git commands from outside of a Git repository

git --git-dir=/Users/meng/my-git-repo/.git --work-tree=/Users/meng/my-git-repo/ status

Remove local branches tracking remote branches that no longer present on remotes

git remote prune origin

Generate a list of my commits

git log --all --no-merges --author='XXX' --stat --after='2015-9-1' | grep -v '^Author: '

Remarks:

  • --all generates the result for all branches;
  • --no-merges ignores the merge commits which are often not desirable in a list of additions and revisions one "genuinely authored";
  • --author='XXX' restricts the list to commits from a specific author;
  • --after='2015-9-1' restricts the list to commits made after the specified date, which is often desirable for listing one's recent commits;
  • grep -v '^Author: ': if --author='XXX' is used, the author information for each entry of commit is redundant.

Find all files committed by a user

git log --no-merges --stat --author="PATTERN" --name-only --pretty=format:"" | sort -u

Remarks:

  • PATTERN is a regex pattern matching the desired user.

Remove ignored files from index

Method 1: git update-index --assume-unchanged

If a file has been added to the index, i.e. the staging area, but it's decided it does not need to be version controlled by Git, but it should also not be deleted from the working tree, remove it from index:

git update-index --assume-unchanged myIgnoredFile

This only removes the myIgnoredFile file from the index of the current repository, but will not remove it from the repositories you push to since it only modified the index of the local repository.

To add it back later,

git update-index --no-assume-unchanged myIgnoredFile

List files that are currently assumed unchanged when computing index:

git ls-files -v | grep "^<span class="createlink">:lower:</span>"

TODO: difference between this method and method 2.

Method 2: git rm --cached

If a file has been added to the index, i.e. the staging area, but it's decided it does not need to be version controlled by Git, remove it from index:

repo/ $ git rm --cached myIgnoredFile

The option --cached limits the removal to the index only and hence the file myIgnoredFile and any modification to it in the working tree is left alone.

Method 3: Method 2 recursively

If there are many files that are added to the index but are then decided to be ignored, and specified by patterns in the .gitignore file, the index can be cleaned as the following

Commit changes that's already staged in the index, so files that are already staged but will not need to be ignored later is properly committed:

git commit -a -m "XXX"

Recursively remove everything from the index

git rm -r --cached .

Re-add everything, so the modified .gitignore file will ensure the ignored files are not added to the index:

git add .

Commit:

git commit -a -m "XXX"

TODO

Always push to multiple remote repositories and pull from one logical central repository

To push to multiple remote repositories automatically everytime you push, but pull only from one remote repository which might be logically considered as the "authoratative" central repository, configure the remote origin as

[remote "origin"]
    url = USER@MYHOST.COM:/PATH//TO/CENTRAL-REPO
    pushurl = USER@MYHOST.COM:/PATH//TO/CENTRAL-REPO
    pushurl = git@gitlab.com:XXXuserXXX/BACKUP-REPO1
    pushurl = https://XXXuserXXX@bitbucket.org/XXXuserXXX/BACKUP-REPO2.git
    fetch = +refs/heads/*:refs/remotes/origin/*

(The all uppercase parts and the parts sandwiched in between two XXXs should be adjusted to one's real-world values.)

  • git pull or git pull origin master will pull from central-repo;

  • git push or git push origin master will push to the logical central repository CENTRAL-REPO as well as the other two backup repositories via the push URLs BACKUP-REPO1 and BACKUP-REPO2. But if the pushurl for the CENTRAL-REPO is not explicitly added, it will not be pushed to, i.e. only having url does not suffice. The repositories other than the logical central repository can be used as back-ups or mirrors.

Rename a branch

Rename a branch:

git branch -m <old_branch_name> <new_branch_name>

Or

git checkout <old_branch_name>
git branch -m <new_branch_name>

Remove old branch from the remote origin [1]:

git push origin --delete <old_branch_name>

or [2]:

git push origin :<old_branch_name>

Push the new branch to the remote origin

git push origin -u <new_branch_name>

References


  1. git-scm.com, "Deleting Remote Branches," 2016, https://git-scm.com/book/en/v2/Git-Branching-Remote-Branches#Deleting-Remote-Branches
  2. <source>:<destination> specifies the refspec to push from and to. When the <source> is omitted, git push origin :<old_branch_name> indicates it pushes an empty refspec to the remote destination branch <old_branch_name> which effectively deletes it on the remote.
blog comments powered by Disqus