Git
Install
sudo apt-get install git
Terminology
- 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.
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