Configuring user information used across all local repositories
git config --global user.name “[firstname lastname]”git config --global user.email “[valid-email]”git config --global color.ui autoConfiguring user information, initializing and cloning repositories
git initgit remote add <shorthand-name> <HTTP_url>
git remote add origin https://github.com/EbenGitHub/FantasticProject.gitgit remote add <shorthand-name> <HTTP_url>
git remote add upstream https://github.com/EbenGitHub/FantasticProject.gitIf we are working on in a project that is someone else, and we fork git to our github repository, and clone it to our local computer. Our local clone will refer to our forked repository as
origin.git remote add origin https://github.com/EbenGitHub/FantasticProject.gitWe can also connect our local clone to the original source repository and it will be refered asupstream.git remote add upstream https://github.com/EbenGitHub/FantasticProject.git
git remote rename <old_name> <new_name>
git remote rename origin mine
git remote rename upstream sourcegit remote git remote -vgit clone [url]Working with snapshots and the Git staging area
git statusgit add [file]or
git add --chmod=+x [file]git reset [file]git diffgit diff --stagedgit commit -m “[descriptive message]”Add exutable permission to the git file
git update-index --chmod=+x [file]Isolating work in branches, changing context, and integrating changes
git branchgit branch [branch-name]git checkoutgit merge [branch]git logundo git adds and git commits
git restore -S ./git reset --soft HEAD~Examining logs, diffs and object information
git loggit log branchB..branchAgit log --follow [file]git log --oneline --graph --decorate --allMASTER -- our local branch head
ORIGIN/MASTER -- remote tracker
git log --statgit shortlog
git shortlogdisplays an alphabetical list of names and the commit messages that go along with them. If we just want to see just the number of commits that each developer has made, we can add a couple of flags:
-sto show just the number of commits (rather than each commit's message) and-nto sort them numerically (rather than alphabetically by author name).
git shortlog -s -nAnother way that we can display all of the commits by an author is to use the regular
git logcommand but include the--authorflag to filter the commits to the provided author.
git log --author=<author_name>
git log --author=SurmaEg. if there are two Surma authors with different last name,
git log --author=Surmawill show commits of both authors If we want to be more specific, we can do:
git log --author="<first_name> <last_name>"
git log --author="Surma Lewis"git show <COMMIT_ID>
git show 5966b66git log --grep=bug
git log --grep bug
git log --grep="unit tests"git diff branchB...branchAgit show [SHA]Versioning file removes and path changes
git rm [file]git mv [existing-path] [new-path]git log --stat -MPreventing unintentional staging or commiting of files
logs/
*.notes
pattern*/matches or wildcard globs.
git config --global core.excludesfile [file]Retrieving updates from another repository and updating local repos
git remote add [alias] [url]git fetch [alias]git merge [alias]/[branch]git push [alias] [branch]
git push <remote-shortname> <branch>
git push origin mastergit pull
git pull <remote-shortname> <branch>
git pull origin masterIf you don't want to automatically merge the local branch with the tracking branch then you wouldn't use
git pullyou would use a different command calledgit fetch. You might want to do this if there are commits on the repository that you don't have but there are also commits on the local repository that the remote one doesn't have either. Git fetch is used to retrieve commits from a remote repository's branch but it does not automatically merge the local branch with the remote tracking branch after those commits have been received.
git fetch
git fetch <remote-shortname> <branch>
git fetch origin mastergit merge <remote-shortname>/<branch>
git merge origin/mastergit push <remote-shortname> <branch>
git push origin masterFork a repository that means you duplicate it Typically you fork a repository that belongs to someone else. So you make an identical copy of their repository and that duplicate copy now belongs to you. This concept of
forkingis also different fromcloning. When you clone a repository, you get an identical copy of the repository. But cloning happens on your local machine and you clone a remote repository. When you fork a repository, a new duplicate copy of the remote repository is created. This new copy is also a remote repository, but it now belongs to you.
git remote add origin <url_forked_repo>git fetch origin mastergit remote add upstream <url_source_repo>git fetch upstream mastergit checkout master
git merge upstream/master
git push origin master
upstream/master- keep track of where the source repository's master branch is.origin/master- keep track of where the forked (our) repository's master branch is.
git pull origin master
git pull upstream masterRewriting branches, updating commits and clearing history
git rebase [branch]git rebase -i HEAD~3
HEAD~2,HEAD~1, andHEADwill merge to one.HEADindicates your current location (it could point to several things, but typically it'll either point to a branch name or directly to a commit's SHA).~3part means "three before", soHEAD~3will be the commit that's three before the one you're currently onHEAD~3will be referenced as a base, but it won't be merged.-imeans interactively We're using this relative reference to a commit in thegit rebasecommand.
don't forget to forcefully push changes to githun. otherwise github won't accept pushes to prevent commit deletion.
git push -fI recommend that you create abackupbranch before rebasing, so that it's easy to return to your previous state. If you're happy with the rebase, then you can just delete the backup branch!
git reset --hard [commit]Temporarily store modified, tracked files in order to change branches
git stashgit stash listgit stash popgit stash dropGitHub Education
EbenGitHub