0%

Git

Reference: Git tutorial, Git docs Git Book

Configuration

1
2
git config --global http.proxy 127.0.0.1:10809
git config --global https.proxy 127.0.0.1:10809
1
2
git config --global user.name sdcgvhgj
git config --global user.email sdcgvhgj@gmail.com
1
git config --global core.editor "nano -w"

where --global means it works for all repositories

SSH key

  1. Generate SSH key:
1
ssh-keygen -o -t rsa -C "sdcgvhgj@gmail.com"
  1. Copy your key:
1
cat /root/.ssh/id_rsa.pub
  1. Add your key at GitHub key settings

Clone

1
git clone https://github.com/USERNAME/REPOSITORY.git

What this does:

  • make new folder
  • initialize as a Git repository
  • create remote origin
  • fetch all branches
  • check out the default branch

Add Remote

1
git remote add upstream REMOTE_URL

add a new remote called upstream

Fetch

1
git fetch REMOTE-NAME

grabs all the new remote-tracking branches and tags without merging those changes into your own branches.

Merge

1
git merge REMOTE-NAME/BRANCH-NAME

merge a remote-tracking branch (i.e., a branch fetched from a remote repository) with your local branch

Pull

1
git pull REMOTE-NAME BRANCH-NAME

complete both git fetch and git merge

use git merge --abort to take the branch back to where it was in before you pulled

Contribute

1
git branch my-branch

create a new branch

1
git checkout my-branch

switch to that branch

1
git add file1.md file2.md

or

1
git add .

stage the changed files

1
git commit -m "edit file1.md, file2.md"

commit changes

Push

1
git push REMOTE-NAME BRANCH-NAME

push your local changes to your online repository

1
git push REMOTE-NAME LOCAL-BRANCH-NAME:REMOTE-BRANCH-NAME

push the LOCAL-BRANCH-NAME to your REMOTE-NAME, but it is renamed to REMOTE-BRANCH-NAME

1
git push -u REMOTE-NAME BRANCH-NAME

-u specifies default branch for future git push and git pull commands

non-fast-forward error:

  • occurs when another person has pushed to the same branch as you

  • pull first before push

Others

git init initialize your repository (create .git file in current folder)

git status shows the status of changes as untracked, modified, or staged

git log --graph show log

git branch -v shows the branches being worked on locally

git branch BRANCH-NAME create branch

git branch -d BRANCH-NAME delete branch

git branch -M BRANCH-NAME rename the current branch to BRANCH-NAME

git remote -v shows remote URLs

git checkout BRANCH-NAME switch branch

git tag list all tags

git reset --hard HEAD throw all uncommitted changes

git commit --amend -m "commit info"

git rebase main rebase current branch to main

git stash save "info" save uncommitted files

git stash pop

git revert

git reflog