configure username
1git config --global user.name "Your Name"
configure email
1git config --global user.email "me@mydomain.com"
check confgiuration
1git config --list
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.
Add files to track
1git add filename.txt
Add all files to track -A
1git add -A
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'
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
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 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>
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.
Merge a branch
1git merge branchName
after manually taking care of merge conflicts,
1git add conflictedFile
and then
1git commit
1git clone remoteRepo yourNewRepoName
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.
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
Screenshot taken from BitBucket. Use git add -A
to add all existing files if you are pushing up an existing project.
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.