Git tips

1
2
Revert a file back up to a commit id
git checkout 887cfc8cbbeb20775c2a0b42ee8fb1a9c5511125 -- yourfile.txT

Tips

  • List branches remotely without cloning the repo
1
2
# Listing remote branches without cloning the repo
git ls-remote -q -h $REPOSITORY | awk '{print $2}' | sed 's/refs\/heads/origin/'
  • Remove a file from the entire repository and history
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# remove DELETE_ME.txt from all branches
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch DELETE_ME.txt' --prune-empty --tag-name-filter cat -- --all

# push changes to git server
git push origin --force --all
git push origin --force --tags

# cleaning orphan objects references (locally)
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now

# Verify any 
evidence of DELETE_ME.txt file
git log --all -- DELETE_ME.txt   | wc -l

Git general use cases

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Reverting changes
git reset --hard/--soft [commit|branch|ref]
git checkout simplefile.txt
git reset . # undo changes and undo `git add`
git reset --soft HEAD~1 # undo last commit

# remove commit from git
git revert --strategy resolve <commit>

# set upstream
git branch --set-upstream dev origin/remoteBranchName

# Merging
git merge -X theirs branchName
git merge -X yours branchName

# Stashing
git stash
git stash list
git stash pop
git stash apply stash@{0}

# Tagging
git tag nameTag <SHA-1>
git tag nameTag branchName
git tag nameTag other tag
git tag nameTag HEAD^

# Patchs
git diff > thediff.patch
git apply -v thediff.patch

# Git diff clean
 git diff -U0 | grep '^[+-]' | grep -Ev '^(--- a/|\+\+\+ b/)' | sed -r "s/^([^-+ ]*)[-+ ]/\\1/"

# Logs and diff
git log --pretty="%h on %ad by %ae => %s" --date=short origin/master
git log --author=darvein@gmail.com --pretty=format:"%h%x09%an%x09%ad%x09%s"
git log --pretty=format:"%h%x09%an%x09%ad%x09%s" --author="Dennis Ruiz"
git-log --graph --online
git log --name-status
git log --name-only
git log --stat
git log <nombre_de_un_branch>..<otro_branch>
git show --pretty="format:" --name-only [aqui el id de un commit]
git log --online --decorate --graph

# Using git subtree
git remote add -f -t master --no-tags gitgit  [https://github.com/git/git.git](https://github.com/git/git.git) 
git subtree add --squash --prefix=third_party/git gitgit/master
git subtree pull --squash --prefix=third_party/git gitgit/master
git subtree push --squash --prefix=third_party/git gitgit/master

# Search a text in all the branches
git log -Stext_to_search --source --all
git config core.filemode false

# Remove a directoryfrom the whole repo
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch NOMBRE_DE_LA_CARPETA_O_ARCHIVO" HEAD
rm -rf .git/refs/original/ && git reflog expire --all &&  git gc --aggressive --prune
git push -f origin master

Setting up a git repo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
sudo adduser git
sudo passwd git

sudo mkdir /srv/git
sudo chown git:git -R /srv/git

su git
ssh-keygen -t rsa
touch ~/.ssh/authorized_keys
chmod 644 ~/.ssh/authorized_keys

cd /srv/git
mkdir properties
cd properties; git init --bare

Got a problem?

  • People were using git merge and rebase to get changes from the parent branch into their feature/hotfix branches. Somehow commits can be lost because of this.
1
2
3
4
git checkout my-broken-branch
git restore --source=origin/master --staged --worktree -- FileThatIdidntTouch.java DirectoryIDidntTouch .fileIdidntTouch
git add -A .
git commit -m "Sync with origin/master remote branch"; git push origin my-broken-branch
  • How to split up a repository?
1
 git filter-branch --prune-empty --subdirectory-filter here/this-directory-on-a-new-repo -- --all
  • Can’t push my commit. It was in github before but it somehow got removed/overwritten, the history is broken idk 😢
1
2
3
4
cp -rf $PWD ../copy-backup
diff -Naur . ../copy-backup > diff.patch
patch -p2 < diff.patch
git status # confirm changes and commit

Github cli

1
2
3
4
5
6
7
8
gh auth login
export GITHUB_TOKEN=ghp_b000000000h

# Clone all git repos
gh repo list gh-org-screening --limit 9999 --json sshUrl | jq '.[]|.sshUrl' | xargs -n1 git clone

# List pull requests
gh pr list --state closed --limit 45 --repo obmedia-admarket/obmedia