Use these links for Mac OS X, Linux, or Windows.
cd mydir
git init
git clone <url>
For example:
git clone https://github.com/twbs/bootstrap.git
cd mydir
git add .
This added all of your files to be commited.
git commit -m "Your commit message"
This commits all files to your local repository.
git push origin master
git remote add <origin_repo_name> <repo_url>
For example:
git remote add my-new-origin git@github.com/my-project
Then you can push:
git push my-new-origin master
There are 3 distinct places where your project state is maintained:
Your actual files, git’s internal index, and the git commits.
Git first adds changes in your files to the index. It then commits the changes recorded in the index.
That means unless you add changes to the index first, new changes will not be included in a commit.
git add myfile.txt
git commit
If you type:
git commit -a
This would do it all at once. It moves all your edits in your files to the index, and then create a new commit.
If you created new files also, you would need to remember to use git add for those before running git commit -a.
git log
One commit per line:
git log --oneline
The top commit:
git show
1 below the top:
git show HEAD^
and you can go further down as such:
git show HEAD^^^
will show you the 4th commit from the top.
Use the commit’s SHAID shown with a number such as e947995881fd7dbfcaf09b6b45965197deb9c769
For example:
git show 5afebb0
Branching means you create a stream of commits under a new label, and the starting point is the point when you create and check out that branch.
git branch staging
git checkout master
git checkout -b staging
git branch -a
git pull origin master
This will pull all updates that has happened in the origin repo (usually a remote repository).
Merging is the reverse operation of branching. Merging means you would append the series of commits (commit history) in one branch on top of all the commits in another branch. Typically the branch being merged from has a common ancestor as the branch being merged into.
You can merge a branch as follows:
git merge staging
or
git pull . staging
Git uses an algorithm to merge the distinct commit histories of 2 branches. It also adds a merge commit at the top to represent the operations it just did to merge those branches.
There is no guarantee that commits from each branch will end up in a sequential order.
A Fast forward is a simple way to append changes that happened in another branch with no merging. The condition is that the current branch has no updates since the branch off, and only the branch we pull from has additional commits to append.
A fast forward sequentially appends all the commits in the 2nd branch since the common ancestor, to the current branch.
git checkout -b staging touch newfile.txt
git add newfile.txt
git commit -m "new file in staging"
At this point we have 1 new commit in staging. Master has no new commits different. Switch to master:
git checkout master
Merge the staging branch with a fast-forward:
git pull . staging
This pulls all changes in staging into master with a fast-forward.
Jan 7
Your Git article is just what I needed! Thanks for putting this together. Just getting started with Git, and this gentle-yet-thorough-enough introduction made learning much easier.