Git
Install
sudo apt-get install git
Terminology
- GitLab - web based interface to your Git repositories (GitHub)
- Staging Area - a place where we can group files together before we "commit" them to Git.
- Commit - is a snapshot of our repository. This way if we ever need to look back at the changes we've made (or if someone else does), we will see a nice timeline of all changes.
File status
- staged: Files are ready to be committed.
- unstaged: Files with changes that have not been prepared to be committed.
- untracked: Files aren't tracked by Git yet. This usually indicates a newly created file.
- deleted: File has been deleted and is waiting to be removed from Git.
- HEAD - is a pointer that holds your position within all your different commits. By default HEAD points to your most recent commit, so it can be used as a quick way to reference that commit without having to look up the SHA.
- Branching - are what naturally happens when you want to work on multiple features at the same time. You wouldn't want to end up with a master branch which has Feature A half done and Feature B half done. Rather you'd separate the code base into two "snapshots" (branches) and work on and commit to them separately. As soon as one was ready, you might merge this branch back into the master branch and push it to the remote server.
Pre requisites
Specify your username and user email as these details are used each time you commit changes
git config --global user.email "piotr@example.com" git config --global user.name "Piotr" git config --global --list #list all of changes you made
Commands
Create new repository in a current directory
git init #creates hidden directory .git/ where changes are recorded all magic happens git status # show files status git log --summary #show details of each commit git diff #see chnages, use HEAD argument for the current position in your commits
Add a file to a staging area for changes to be tracked
git add octocat.txt git add -A #add all files git add '*.txt' #add files using a wildcard, notice that quotes are required git reset <filename> #remove a file from staging area
Committing The files are in the Staging Area, are not in a repository yet. To store our staged changes we run:
git commit -m "Description of what we changed"
Add local repository to remote GitHub server
creates repo name on remote GitHub server and repository git remote add origin https://github.com/yourgitspace/gitrepo.git git push -u origin master #push commits to origin repo master branch, -u stores these parameters
Pull changes
git pull origin master #from origin repo master branch git stash # to stash your not committed changes before pull git stash apply # to re-apply your changes after your pull
Preview changes of your staged files
git diff --staged
Revert a file version from the last commit
git checkout -- filename.txt
Create a new branch space to work for us
git branch branchname-newcode git branch -d branchname-newcode #deletes branch git branch -D branchname-newcode #forces delete since you cannot delete a branch that has not been merged, -D is equivalent -d -f
Change branch
git branch #list branches, one with asterisks it active current branch git checkout branchname-newcode #change to new branch
Remove files
git rm '*.txt' git rm -r folder_of_cats #remove folders recursively git commit -am "Delete stuff" #-a auto removes deleted files within commit
Merge other branch with the current active branch
git merge branchname-newcode
Git config - configuration
Git comes with a tool called git config that lets you get and set configuration variables that control all aspects of how Git looks and operates. These variables can be stored in three different places:
/etc/gitconfig
contains values for every user on the system and all their repositories. Option --system reads this file~/.gitconfig
or~/.config/git/config
specific to your user, --global option writes to this file.git/config
config file in the Git directory of whatever repository you’re currently using: option --local is specific to that single repository.
Each level overrides values in the previous level, so values in <.git/config trump those in /etc/gitconfig.
On Windows systems, Git looks for the .gitconfig file in the $HOME directory (C:\Users\$USER for most people). It also still looks for /etc/gitconfig, although it’s relative to the MSys root, which is wherever you decide to install Git on your Windows system when you run the installer. If you are using Git for Windows 2.x or later, there is also a system-level config file at C:\Documents and Settings\All Users\Application Data\Git\config on Windows XP, and in C:\ProgramData\Git\config on Windows Vista and newer. This config file can only be changed by git config -f <file> as an admin.
Resources
- Git training
- My profile at Code School
- Atom cross platform text editor that syncs with Git
- Git workflow
- Error: Permission denied (publickey) Error explained
- Build GitLab repo web based interface