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 auto
Configuring user information, initializing and cloning repositories
git init
git remote add <shorthand-name> <HTTP_url>
git remote add origin https://github.com/EbenGitHub/FantasticProject.git
git remote add <shorthand-name> <HTTP_url>
git remote add upstream https://github.com/EbenGitHub/FantasticProject.git
If 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.git
We 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 source
git remote
git remote -v
git clone [url]
Working with snapshots and the Git staging area
git status
git add [file]
or
git add --chmod=+x [file]
git reset [file]
git diff
git diff --staged
git 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 branch
git branch [branch-name]
git checkout
git merge [branch]
git log
undo git adds and git commits
git restore -S ./
git reset --soft HEAD~
Examining logs, diffs and object information
git log
git log branchB..branchA
git log --follow [file]
git log --oneline --graph --decorate --all
MASTER -- our local branch head
ORIGIN/MASTER -- remote tracker
git log --stat
git shortlog
git shortlog
displays 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:
-s
to show just the number of commits (rather than each commit's message) and-n
to sort them numerically (rather than alphabetically by author name).
git shortlog -s -n
Another way that we can display all of the commits by an author is to use the regular
git log
command but include the--author
flag to filter the commits to the provided author.
git log --author=<author_name>
git log --author=Surma
Eg. if there are two Surma authors with different last name,
git log --author=Surma
will 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 5966b66
git log --grep=bug
git log --grep bug
git log --grep="unit tests"
git diff branchB...branchA
git show [SHA]
Versioning file removes and path changes
git rm [file]
git mv [existing-path] [new-path]
git log --stat -M
Preventing 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 master
git pull
git pull <remote-shortname> <branch>
git pull origin master
If you don't want to automatically merge the local branch with the tracking branch then you wouldn't use
git pull
you 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 master
git merge <remote-shortname>/<branch>
git merge origin/master
git push <remote-shortname> <branch>
git push origin master
Fork 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
forking
is 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 master
git remote add upstream <url_source_repo>
git fetch upstream master
git 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 master
Rewriting branches, updating commits and clearing history
git rebase [branch]
git rebase -i HEAD~3
HEAD~2
,HEAD~1
, andHEAD
will merge to one.HEAD
indicates 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).~3
part means "three before", soHEAD~3
will be the commit that's three before the one you're currently onHEAD~3
will be referenced as a base, but it won't be merged.-i
means interactively We're using this relative reference to a commit in thegit rebase
command.
don't forget to forcefully push changes to githun. otherwise github won't accept pushes to prevent commit deletion.
git push -f
I recommend that you create abackup
branch 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 stash
git stash list
git stash pop
git stash drop
GitHub Education
EbenGitHub