Skip to content

foyez/git

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 

Repository files navigation

Git

Check Git configuration:

git config -l

Git configuration:

# setup user info globally
git config --global user.name "username"
git config --global user.email "username@email.com"

# setup user info locally (specific git repository)
git config user.name "username"
git config user.email "username@email.com"

# 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

About

Git at a glance

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published