Repository navigation
My Git Handbook
Asraful Samad Chowdhury edited this page Oct 23, 2021
·
2 revisions
$ cd /path/to/my/codebase
$ git init (1)
$ git add . (2)
$ git commit (3)
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
git branch update-readme
git checkout update-readme
git checkout -b update-readme
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 -m "create the contributing guide"
git commit -am "descriptive commit message"
git commit --amend: Replaces the most recent commit with a new commit.
git revert
git reset
git reset HEAD (aka git reset --mixed HEAD)
git reflog
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 -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: 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.
git reflog
git reset --hard <SHA>
git fetch <remote> <branch>
ex: git fetch origin master
git reset --hard <remote>/<branch>
ex: git reset --hard origin/master
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
$ cat .gitignore
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar