Git Basics
Git overview
- Distributed Version Control
- Data Consistency and Integrity
- Performance - Diffs are local therefore quicker, do not have to go over the network to perform diffs.
- Merging - Automatic merge if there are no conflicts (more than one change at the same position of the modified file).
- Conflicts have to be merged manually.
Git commands
Clone a repository locally from the remote server
Create new repository
Add a new repository on github online - ‘demo’. Then create a local directory - ‘demo’ and navigate to it through the command line.
Git settings
Git settings are defined in a .gitconfig file which is located in %USERPROFILE% on Windows and $HOME on Unix based machines.
List the config settings
Set the default username and email globally for the entire system
In order to override this with a different name or e-mail address for specific projects, you can run the command without the –global option when you’re in that project.
If git prompts you for a username and password you could be using the HTTPS clone for your repository instead of your SSH key passphrase. The SSH based URL looks like - git@github.com:user/repo.git. Follow the steps below to switch it to SSH.
Verify the remote that a local repo is mapped to
Verify the remote URL that a local repo is mapped to
Change the remote URL that local repo is mapped to
Push all local branches that have the same name on the remote. This means potentially many different branches will be pushed, including those that you might not even want to
Push only the current working branch if it has the same name on remote
Branches
Get a list of all the branches
Create a branch and switch to it at the same time
Create a branch, switch to it and set the tracking/upstream branch
$ git checkout -b
Merge with a specified branch
Delete a remote branch
Delete a local branch
Track a remote branch e.g. track remote master with local master
Compare a file in 2 branches
Compare the commits in 2 branches
Undoing changes
$ git reset head –hard (Moves the head to the most recent commit and erase all the changes in your working tree)
Find a commit that you undid, it is typically present in git reflog unless you have garbage collected your repository
Remove untracked (git ignored) files and directories
-f option required for overriding permissions
-d also remove the directories
-X just remove ignored files (for example - files in build dir: bin/, user setting files etc.)
-x remove ignored as well as non-ignored files
Amend a commit
Ensure all changes are staged
Amend a commit with no changes to the message
Remove local commits
Revert the commit without creating another commit
Remove local commit and reset the head to the same as remote branch
Remove local commit but keep changes
Rearrange commits
Abort a rebase
Move to an old commit
Suppose the old commit that you want to go back to was HEAD@{5} in the ref log
Go back to the previous commit
Finding commits
Find a commit with text “text to find”
Create patch
Creating a patch
Stash your work
Stash work with comment
List the stashes
Apply a stash
Drop a stash
Create a branch from a stash
This creates a new branch for you, checks out the commit you were on when you stashed your work, reapplies your work there, and then drops the stash if applied successfully.
Tags
List all the tags
Create a tag
By default, the git push command doesn’t transfer tags to remote servers. Push tags to a shared server after you have created them
Push all of your tags to the remote server that are not already there
Adding SSH key to the ssh-agent
$ ssh-add ~/.ssh/id_rsa
On trying to add the SSH key to the ssh-agent you can get the following error: ‘Could not open connection to your authentication agent’. You need to start ssh-agent before you can run the ssh-add command by running:
$ eval `ssh-agent -s`
Once the key has been added git should not ask for the key pass phrase on every push/pull.