github Flashcards
Version
git –version
Workflow
- git add .
- git commit -m [message]
- git push
check status of files
git status
Staging unstaged or untracked files
git add filename
git add dirname
git add .
committing staged files
git commit -m “message.”
What does SSH stand for?
Secure shell
checking for ssh file in project
in git bash:
ls -a -l ~/.ssh
checking for ssh file in project
in git bash:
ls -a -l ~/.ssh
How to generate an SSH key
in git bash, in the root directory:
ssh-keygen -t rsa -b 4096 -C “email address”
Concerning ssh keys, how can you get the ssh pid?
in git bash:
eval “$(ssh-agent -s)”
How to register ssh key or file?
in git bash:
ssh-add ~/.ssh/id_rsa
How to view current remote origins
git remote -v
How to remove a remote origin or destination
git remote rm origin
git remote rm destination
How to add a remote origin?
git remote add origin git@github.com:accountname/reponame
How to push repository to github?
git push -u origin master
How to acces the key of an .ssh file?
in git bash:
cat ~/.ssh/id_rsa.pub
How to test ssh connection?
in git bash:
ssh -T git@github.com
How to push to heroku?
git push heroku master
SSH key workflow
- check for existing key: ls -a -l ~/.ssh
- Generate a key: ssh-keygen -t rsa -b 4096 -C “email
address” - Check if keygen was succesful: eval “$(ssh-agent -s)”
- Register key: ssh-add ~/.ssh/id_rsa
- Access key in file: cat ~/.ssh/id_rsa.pub
- Setup SSH with provider
- Test connection: ssh -T git@github.com
General Git workflow
- git add .
- git commit -m “message.”
- git push origin master
How to clone a specific branch
git clone –single-branch –branch
Difference between git clone and git pull?
git clone is how you get a local copy of an existing repository to work on. It’s usually only used once for a given repository, unless you want to have multiple working copies of it around. (Or want to get a clean copy after messing up your local one…)
git pull (or git fetch + git merge) is how you update that local copy with new commits from the remote repository. If you are collaborating with others, it is a command that you will run frequently.
Difference between clone, pull, fork, and push
git clone means you are making a copy of the repository in your system.
git fork means you are copying the repository to your Github account.
git pull means you are fetching the last modified repository.
git push means you are returning the repository after modifying it.
In layman’s term:
git clone is downloading and git pull is refreshing.
How to see a list of git commits?
git log
How to clone a git repository into a manually named directory?
git clone [url] [dirName]
How can we display only the commits of log messages?
git log –oneline
git log flag that shows which files have changed
git log –stat
Which log flag show exactly which changes have been made
git log -p or git log –patch
How to see a specific commit
git log [SHA] -p
git show [SHA]
Do’s and Don’ts for commit messages
Do
- do keep the message short (less than 60-ish characters)
- do explain what the commit does (not how or why!)
Do not
-do not explain why the changes are made (more on this below)
-do not explain how the changes are made (that’s what git log -p is for!)
-do not use the word “and”
if you have to use “and”, your commit message is probably doing too many changes - break the changes into separate commits
e.g. “make the background color pink and increase the size of the sidebar”
The best way that I’ve found to come up with a commit message is to finish this phrase, “This commit will…”. However, you finish that phrase, use that as your commit message.
Above all, be consistent in how you write your commit messages!
Which command can be used to see changes that have been made but haven’t been committed, yet?
git diff
How to display tags
git tag
how to delete a tag
git tag -d [tag name]
git tag –delete [tag name]
How to add a tag
git tag -a [tag name] (the stands for annotate)
How to create a new branch
git branch [branchName]
How to move the head pointer to a specific branch
git checkout [branchName]
How do we delete a branch?
git branch -d [branchName]
How to add a branch at a specific commit?
git branch [branchName] [SHA-abreviated]
How to create a new branch and check it out at the same time?
by useing the -b flag
git checkout -b [branchName]
How to see logs of all branches at the same time
git log –oneline –decorate –graph –all
How to merger a branch with the master branch
git merge [branchName]
How to undo a merge?
git reset –hard HEAD^
How to ammend the most recent commit message
git commit –amend
How to undo file changes that has been committed?
git revert [sha]
Reset vs Revert
At first glance, resetting might seem coincidentally close to reverting, but they are actually quite different. Reverting creates a new commit that reverts or undos a previous commit. Resetting, on the other hand, erases commits!
Relative Commit References
You already know that you can reference commits by their SHA, by tags, branches, and the special HEAD pointer. Sometimes that’s not enough, though. There will be times when you’ll want to reference a commit relative to another commit. For example, there will be times where you’ll want to tell Git about the commit that’s one before the current commit…or two before the current commit. There are special characters called “Ancestry References” that we can use to tell Git about these relative references. Those characters are:
^ – indicates the parent commit
~ – indicates the first parent commit
Here’s how we can refer to previous commits:
the parent commit – the following indicate the parent commit of the current commit
HEAD^
HEAD~
HEAD~1
the grandparent commit – the following indicate the grandparent commit of the current commit
HEAD^^
HEAD~2
the great-grandparent commit – the following indicate the great-grandparent commit of the current commit
HEAD^^^
HEAD~3
The main difference between the ^ and the ~ is when a commit is created from a merge. A merge commit has two parents. With a merge commit, the ^ reference is used to indicate the first parent of the commit while ^2 indicates the second parent. The first parent is the branch you were on when you ran git merge while the second parent is the branch that was merged in.
How to delete a recent commit
git reset
git reset HEAD^
Flags for git reset
- -mixed (default) (moves commit to working directory)
- -soft (moves commit to staging index)
- -hard (moves commit directly to trash)
How to make a backup of branch before attempting to reset it, and then restor the backup?
git branch backup
then when need to restor simple checkout the master and git merge backup
How to pull remote repo down to local computer?
git pull origin master
When would you use git fetch instead of git pull?
When you dont want to automatically merge the local branch with the tracking branch.
Git pull automatically does a forward merge with tracking branch
What if there the remote is several commits ahead of the local repo and there are some commits in the local repo that has not yet been pushed up to the remote?
git fetch origin master
git merge origin/master
git push origin master
what is git fork
git fork, makes a identical copy of a repo after which you become the owner of that copy
How to see a short list of commits by individual contributers?
git shortlog
Which flags can be added to 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).
how to filter logs by a particular author
git log –author=Surma
How to find a commit that references a particular term?
git log –grep=[term]
Workflow for contributing to other developers repos
- fork repo
- clone to working directory
- check-out new branch
- commit changes
- push to forked repo
how to setup an upstream repo
git remote add upstream [url]
How to rename remotes
git remote rename mine origin
How to combine a number of commits into one larger commit?
git rebase
example:
git rebase -1 HEAD~3
NB: before squashing commits, you should first create a backup branch
Rebase commands
use p or pick – to keep the commit as is
use r or reword – to keep the commit’s content but alter the commit message
use e or edit – to keep the commit’s content but stop before committing so that you can:
add new content or files
remove content or files
alter the content that was going to be committed
use s or squash – to combine this commit’s changes into the previous commit (the commit above it in the list)
use f or fixup – to combine this commit’s change into the previous one but drop the commit message
use x or exec – to run a shell command
use d or drop – to delete the commit