git config --global user.name "Your Name"
git config --global user.email "firstname.lastname@example.org"
git config --list
Start a git repo
git init my_new-repo
Start a git repo in existing folder, cd to that folder and..
Removing the repo = just remove the .git folder, it’s the brains
rm -r my_project/.git
where my_project is the project folder you are removing git from.
Add files to track
git add filename.txt
Add all files to track
git add -A
Adding a file
git add README.md
where README.md is the name of your file.
Commit all code
git commit -a
Commit all code with a message
git commit -a -m 'your commit message'
show git status
show git log for a history of commits
show details of a specific commit Method 1: checkout
git checkout commitIDENTIFIER
you dont need the whole identifer, the first 5 letters would usually do.
go back to where you were after checking out a commit
git checkout master
master is the branch you were previously on before checking out the commit.
Method 2: diff check difference between two commits
git diff commit1 commit2
To remove files/folders from a git repo
git rm filename.txt
git rm -r directory/cache
-f flag if you have to force it. It is recommended to add files you don’t want in the repo to .gitignore so they don’t get added in the first place.
git branch -D branchName
-D flag will delete the branch. you can’t delete the branch you are in, so switch before deleting.
Bracnhes are like alternate realities for your repositry. They let you pursue different courses of action on your project in parallel.
Check what branch you are in
Create a new branch
git branch branchName
Switch to a branch
git checkout branchName
Switch back to master branch
git checkout master
Switch and create branch in the same command
git checkout -b newFeature
-b flag will create the branch if it doesnt already exists.
* asterisk indicates what branch you’re currently on.
Merge a branch
git merge branchName
after manually taking care of merge conflicts,
git add conflictedFile
git clone remoteRepo yourNewRepoName
Working with Remotes
show list of remote repos
add remote repo
git remote add NameForRemote Location/url_of_remote
add github repo
git remote add origin github_url
the name doesn’t have to be origin, it’s just convention to name it that
push changes to remote
git push -u origin master
origin is the name of remote repo and
master is the branch.
installing git-flow on Linux
sudo apt-get install git-flow
start git flow
git flow init
start git flow on a new branch
git flow Flow_banch start BranchName
close git flow on a new branch
git flow Flow_banch finish BranchName
Starting from Scratch or Uploading an Existing Project
Screenshot taken from BitBucket. Use
git add -A to add all existing files if you are pushing up an existing project.
- Install the git-credential-osxkeychain helper
- So that you don’t have to enter login details everytime you push from Mac over HTTPS.
- Set up SSH for Git and Mercurial on Mac OSX/Linux
- So you can communicate with git server (Bitbucket) over SSH.
- The most important part in the repo is the .git folder. The .git folder is what tracks everything. The name of the folder you initiate a git repo with/in doesn’t matter. The files inside doesn’t matter. Whether you delete all those files doesn’t matter either. If you have the .git folder intact, you’ll be able to restore all those files
- –global flag means that we’d like to apply these changes for all our repositories
- the answer to ‘should i commit?’ is ‘do you have a good commit message?’
- when you add a file, git adds it to what’s called the staging area.
- check this link for a better commit log.
- the most recent commit = HEAD
- the commit before the last commit = HEAD~1
- master is like the trunk of your project. It’s usually the main code of your project that’s deployed.
- when we create a branch, it starts with a copy of the branch we are in. If you are in a branch other than master, your new branch will copy the branch you are currently in. Consider actual tree branches, they branch out from where they are already.
For a more detailed, easy to understand and video demonstrated intro to Git, take the Git Basics course on Treehouse. This article was written while taking that course.