Notes

Git Basics

Edit on GitHub

Git & Github
4 minutes

Configuration

configure username

1git config --global user.name "Your Name"

configure email

1git config --global user.email "me@mydomain.com"

check confgiuration

1git config --list

Repositories

Start a git repo

1git init my_new-repo

Start a git repo in existing folder, cd to that folder and..

1git init

Removing the repo = just remove the .git folder, it’s the brains

1rm -r my_project/.git

where my_project is the project folder you are removing git from.

Adding Code

Add files to track

1git add filename.txt

Add all files to track -A

1git add -A

Committing Code

Adding a file

1git add README.md

where README.md is the name of your file.

Commit code

1git commit

Commit all code -a

1git commit -a

Commit all code with a message -m

1git commit -a -m 'your commit message'

Check Status

show git status

1git status

show git log for a history of commits

1git log

show details of a specific commit Method 1: checkout

1git 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

1git checkout master

master is the branch you were previously on before checking out the commit.

Method 2: diff check difference between two commits

1git diff commit1 commit2

Removing code

To remove files/folders from a git repo

1git rm filename.txt

OR

1git rm -r directory/cache

Use -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.

Delete branches

delete a branch locally

1git branch -d FOO
2git branch --delete FOO
3
4git branch -D FOO
5git branch --delete --force FOO

-d is the same as --delete and -D is the same as --delete --force

-D or --delete will delete the branch locally. you can’t delete the branch you are in, it’ll ask you to switch before deleting.

delete a remote branch

1git push <remote_name> --delete <branch_name>
2
3git push <remote_name> :<branch_name>

Branches

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

1git status

Create a new branch

1git branch branchName

Switch to a branch

1git checkout branchName

Switch back to master branch

1git checkout master

Switch and create branch in the same command

1git checkout -b newFeature

-b flag will create the branch if it doesnt already exists.

show bracnhes

1git branch

* asterisk indicates what branch you’re currently on.

Merging

Merge a branch

1git merge branchName

after manually taking care of merge conflicts,

1git add conflictedFile

and then

1git commit

Cloning

1git clone remoteRepo  yourNewRepoName

Working with Remotes

show list of remote repos

1git remote

add remote repo

1git remote add NameForRemote Location/url_of_remote

add github repo

1git 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

1git push -u origin  master

origin is the name of remote repo and master is the branch.

Git Flow

installing git-flow on Linux

1sudo apt-get install git-flow

start git flow

1git flow init

start git flow on a new branch

1git flow Flow_banch start BranchName

close git flow on a new branch

1git flow Flow_banch finish BranchName

Starting from Scratch or Uploading an Existing Project

BitBucket Project help

Screenshot taken from BitBucket. Use git add -A to add all existing files if you are pushing up an existing project.

Further Reading

Notes

  • 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.

Related