Git commands Manual (Continually updated)

GeekerHWH | Feb 26, 2024 min read

user configuration

git config --global user.name "GeekerHWH"
git config --global user.email "xxxxxxxxxx@gmail.com"

you can check the settings with:

git config --list

to set users for each repository seperately

git config --local user.name "GeekerHWH"
git config --local user.email "xxxxxxxxxx@gmail.com"

set alias

for example:

git config --global alias.co checkout
git config --global alias.st status
git config --global alias.l "log --oneline --graph"

.gitignore

# .gitignore

# ignore secret.yml
secret.yml

#ignore db.yml in config
config/db.yml

# ignore the suffix
/db/*.sqlite3

repository operations

initialize repository

git init -b main

add all and commit

git commit -a -m "xxx"

remove tracking without deletion

git rm file_name --cached

amend the message of last commit

git commit --amend -m "new meassage"

amend the file of last commit

git add file
git commit --amend --no-edit # no edit means no change to the message

Rescue accidentally deleted files

git checkout file

check commit history

user-friendly output

git log --graph

to find someone’s commit

git log --author="A"
git log --author="A\|B" # A or B

to find key word

git log --grep="LOL"

to find commits during a period of time

git log --since="9am" --until="12am"

check who wrote this line of code

git blame file

branchs operations

Display the branch in the terminal

append the following content to the .bashrc file in your home directory

function git_branch {
   branch="`git branch 2>/dev/null | grep "^\*" | sed -e "s/^\*\ //"`"
   if [ "${branch}" != "" ];then
       if [ "${branch}" = "(no branch)" ];then
           branch="(`git rev-parse --short HEAD`...)"
       fi
       echo " ($branch)"
   fi
}

export PS1='\u@\h \[\033[01;36m\]\W\[\033[01;32m\]$(git_branch)\[\033[00m\] \$ ' 

then every time you enter a directory with a git repo initialized the terminal will be look like this fancy stuff: branchsInTerminal.png

create new branch

git branch new_branch

create new branch and point it to any commit

git branch new_branch [SHA-1]

rename branch

git branch -m branch_name new_name

force delete branch

git branch -D branch_name

switch to another branch

git checkout another_branch

merge branchs

for example, you want to merge test into main, make sure you are at main then:

git merge test

rebase branch

git rebase test

Git & Github

add remote repository endpoint

git add remote origin xxx@github.com:xxx/xxxxxxxxxx.git # "origin" is the name of this endpoint

push

git push -u origin main # -u set the origin as the main's upstream, you can use git push to do the same thing next time

change remote endpoint

git remote set-url <name> <newurl> # git remote set-url origin xxxxx.git

Get latest released version from Github

https://api.github.com/repos/${AuthorName}/${ProjectName}/releases/latest

Theoriodical questions in Interview(Continually updated)

Uncommon Usage

To solve long time clone

Given that we are in the main branch without any other branch, we’ve commited and pushed literally hundreds and thousands times. In this situation, we may take pretty much long time to clone the repo from remote(Github). Here are some solutions:

clean commit history

  1. create a brand new branch with --orphan parameter, which only keeps the final commit node
    git checkout --orphan new_branch
    
  2. add and commit
    git add .
    git commit -m "very new version"
    
  3. delete the original main branch and rename the present branch
    git branch -D main
    git branch -m main # change new_branch's name to "main"
    
    now your local repo has delete all commit history
  4. force push to clean remote commit history
    git push -f origin main
    

    P.S. some repositories have branch protection, you need to turn it off first.

comments powered by Disqus