Code Monkey home page Code Monkey logo

git's Introduction

Git

Check Git configuration:

git config -l

Git configuration:

# setup user info globally
git config --global user.name "username"
git config --global user.email "[email protected]"

# setup user info locally (specific git repository)
git config user.name "username"
git config user.email "[email protected]"

# edit global git configuration
git config --global --edit 

# set default branch name of git initialization
git config --global init.defaultBranch <name>

Cache Login credential:

git config --global credential.helper cache

Initialize git

git init

Stage files

git add file_name # add a file
git add * # add all files
git add fil* # add all files starting with 'fil'

Check repository status

git status

Commit changes

git commit # commit in editor
git commit -m "your message" # commit with message
git commit -am "your message" # add & commit tracked files

Commit history

git log # see commit history
git log -p # see commit history including changes
git log --stat # see commit history including line(s) changed and file names
git log --graph --oneline # show log graph of a branch
git log --graph --oneline --all # show log graph of all branches

See changes made before committing

git diff # all unstaged changes
git diff file_name # unstaged changes of a specific file
git diff --staged

Remove tracked files

git rm filename

Remove tacked files/directories that was tracked already

git rm --cached <file>
git rm -r --cached <folder> # for directory

Rename files

git mv old_file new_file

Revert unstaged changes

git checkout filename # revert unstaged changes from specified file
git checkout -- . # revert unstaged changes from all files

Revert staged changes

git reset HEAD filename
git reset HEAD~2 # revert the last two commits
git reset --hard HEAD~1 # revert the last commit and discard the changes
git reset HEAD -p # specify the changes you want to reset

Amend the most recent commit

# should avoid amending pushed commits
git commit --amend # modify and add changes to the most recent commit
git commit --amend -m "your message"

Undo last commit

git reset HEAD~
or 
git reset HEAD~1

Rollback an old commit

git revert commit_id # create a new commit to revert the old changes

Create & switch branch

git branch branch_name
git checkout -b branch_name # create a new brach & switch to new created branch
git checkout branch_name

Show branch list

git branch

Rename a branch

git branch -M <name>

Delete a branch locally & remotely

Delete a local branch:

git branch -d <branch_name>

Delete a remote branch:

git push <remote_name> -d <branch_name> # remote_name is normally origin

Switch branch & keep changes

  1. switch with creating new branch
git switch -c "new_branch_name"
  1. switch without creating new branch
git stash save
git checkout branch
git stash pop

Merge two branches

git merge branch_name # merge active branch with branch_name
git merge --abort # abort a conflicting merge

Add a remote repository

git add remote remote_repo_url

See remote URLs

git remote -v
git remote show origin # get more info about remote repo

Interact with remote repo

git push -u origin branch_name # push a new branch to a remote repo
git push # push changes to a remote repo
git push -f # force a push request
git fetch # download the latest changes from a remote repo
git pull # download the latest changes from a remote repo and merge them with local branch
git pull -r # download the latest changes from a remote repo and rebase them with local branch
git branch -r # check remote branches

Resolve a conflict

git pull -r
# Resolve conflicts which changes you want to add
git add .
git rebase --continue

Git rebase (transfer completed work from one branch to another)

git rebase branch_name

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.