My Git Handbook - AsrafulSamad/PoraLikha GitHub Wiki

Start a new Git repository for an existing code base


$ cd /path/to/my/codebase
$ git init      (1)
$ git add .     (2)
$ git commit    (3)

Git Clone

git clone [url]  ::  git clone https://github.com/github/training-kit.git
git clone [url] --branch [branch] --single-branch
git branch
git branch --all
git pull = git fetch + git merge
git push: Uploads all local branch commits to the remote
git remote -v: Show the associated remote repositories and their stored name, like origin

Create a branch

git branch update-readme
git checkout update-readme
git checkout -b update-readme

Git Add

git add [filename]
git add <path>: Stage a specific directory or file
git add .: Stage all files (that are not listed in the .gitignore) in the entire repository
git add -p: Interactively stage hunks of changes
git add -u
git push -u origin update-readme

Git Commit

git commit -m "create the contributing guide"
git commit -am "descriptive commit message"
git commit --amend: Replaces the most recent commit with a new commit.

Using git reset to undo git add

git revert
git reset
git reset HEAD (aka git reset --mixed HEAD)
git reflog

git log/status

git log
git status: Most often used in its default form, this shows a good base of information
git status -s: Give output in short format
git status -v: Shows more "verbose" detail including the textual changes of any uncommitted files

Git Remote

git remote -v: List the current remotes associated with the local repository
git remote add [name] [URL]: Add a remote
git remote remove [name]: Remove a remote
git fetch --prune : To delete the remote tracking branches that are deleted on the remote
git pull: Updates your current local working branch with all new commits from the corresponding remote branch on GitHub. git pull is a combination of git fetch and git merge.

git pull

git pull: Update your local working branch with commits from the remote, and update all remote tracking branches.
git pull --rebase: Update your local working branch with commits from the remote, but rewrite history so any local commits occur after all new commits coming from the remote, avoiding a merge commit.
git pull --force: This option allows you to force a fetch of a specific remote tracking branch when using the <refspec> option that would otherwise not be fetched due to conflicts. To force Git to overwrite your current branch to match the remote tracking branch, read below about using git reset.
git pull --all: Fetch all remotes - this is handy if you are working on a fork or in another use case with multiple remotes.

Undo A git pull

git reflog
git reset --hard <SHA>

Force git pull to Overwrite Local Files

git fetch <remote> <branch>
ex: git fetch origin master
git reset --hard <remote>/<branch>
ex: git reset --hard origin/master

Common usages and options for git push

git push -f: Force a push that would otherwise be blocked, usually because it will delete or overwrite existing commits (Use with caution!)
git push -u origin [branch]: Useful when pushing a new branch, this creates an upstream tracking branch with a lasting relationship to your local branch
git push --all: Push all branches
git push --tags: Publish tags that aren't yet in the remote repository

gitignore

$ cat .gitignore
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar

References:

  1. https://ndpsoftware.com/git-cheatsheet.html#loc=stash;
  2. https://www.atlassian.com/git/tutorials/atlassian-git-cheatsheet
  3. https://education.github.com/git-cheat-sheet-education.pdf
  4. https://about.gitlab.com/images/press/git-cheat-sheet.pdf
⚠️ **GitHub.com Fallback** ⚠️