The getting started recipe
echo "# git-workshop" >> README.md git init git add README.md git commit -m "first commit" git remote add origin email@example.com:josdem/git-workshop.git git push -u origin master
git initCreate a new git repository
git addAdd a new file
git commit -mCommit changes from a single file
git remote addAdd a new remote repository
git pushPush your commits to remote repository
In order to develop features isolated from each other we can use branches, repositories consists of “trees” maintained by git. So you have master as main tree and you will be able to create as many branches as you want. Use branches for development and merge them back to the master upon completion.
git checkout -b feature/1
With previous command we created a new feature branch, usually we create a branch for each new feature we are working. So let’s create a new file called
# Chapter 1 When I was six years old, I saw, once, a splendid picture.
Then let’s add that file to our local repository
git add Chapter_1.md gc -m "#1 Adding Chapter 1 file"
Now let’s add more content to our file
# Chapter 1 When I was six years old, I saw, once, a splendid picture, in a book about the virgin forest called *Stories of Life*. It represented a boa snake that swallowed a wildcat. This is the copy of the drawing. ![Snake Swallowed a Wildcat](/img/snake_swallowed_wildcat.png)
And then other commit
git commit -m "#1 Completing first paragraph"
Here we are adding “#1 …” as feature reference. You can continue adding and committing changes with descriptive comments, try to do small changes for each commit so in that way will be easier to do code reviews. Once you complete your feature you can merge that changes to master.
git checkout master git merge feature/1 git push origin master
You can create a fresh copy of a local repository by running this command
git clone firstname.lastname@example.org:josdem/git-workshop.git
At this point you have only master, you can see that with this command:
* master (END)
In order to update remote tracking branches you can use this command
Then you can use this command to move to the fresh copy of
git checkout feature/1
Branch 'feature/1' set up to track remote branch 'feature/1' from 'origin'. Switched to a new branch 'feature/1'
Now we should have two local branches ready
* feature/1 master (END)
Delete a branch
You can delete a local branch with this command
git branch -d feature/1
If you want to delete a remote branch you can use this command
git push origin --delete feature/1
To incorporate changes from a remote repository into the local branch use this command
git pull origin branch_name
git pull, updates your current local branch with the latest changes from the remote server.
git fetch never change or update any of your own local branches, it a is safe operation.
Git status command displays the state of the working directory.
It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git.
Ignored files are usually build directories and generated files. It is a good practice to ignore those files, you simply need to create a file named
.gitignore and add path plus file names.
credentials.yml .classpath build/ *.log **swp
credentials.ymlIs a personal file with your username and password
.classpathA computer generated file
build/A computer generated directory
*.logAn asterisk is a wildcard that matches zero or more characters
**swpDouble asterisk to match directories anywhere in the repository
The simpliest way to see your repository history is using this command:
commit 57b3fcd5985d5a905690154ff37d83753b2a0fcc Author: josdem <email@example.com> Date: Sat Feb 23 09:24:32 2019 -0500 #1 Adding image commit fc5266ceb809a161d82db3b54c906efac846c1e3 Author: josdem <firstname.lastname@example.org> Date: Sat Feb 23 09:10:43 2019 -0500 #1 Changing file name commit 33d8a5feb6329c87f525685b39ce5e9b076cd3f3 Author: josdem <email@example.com> Date: Sat Feb 23 09:07:58 2019 -0500 #1 Deleting Chapter 1 in txt format commit 89365cd47aee0a534f2bb65fd1ebbd1600f6d0e3 Author: josdem <firstname.lastname@example.org>
To see only the commits of a certain author
git log --author=josdem
Compressed log where each commit has a single line
git log --pretty=oneline
57b3fcd5985d5a905690154ff37d83753b2a0fcc #1 Adding image fc5266ceb809a161d82db3b54c906efac846c1e3 #1 Changing file name 33d8a5feb6329c87f525685b39ce5e9b076cd3f3 #1 Deleting Chapter 1 in txt format 89365cd47aee0a534f2bb65fd1ebbd1600f6d0e3 #1 Adding Chapter 1 file 3929d19b3183f36a2c22350b621267142d0f713d #1 Adding first line in Chapter 1 b2fefdbc93de041541b2c1cda0da26e8369a9cd8 #1 Adding Chapter 1 306b0c78b8062ed59c81d3262916d827f61a64f7 Adding Git ignore file 52d856f9c2b44352abd77487891cdfba4af0f3b9 first commit
In case you did something wrong, you can replace local changes using this command
git checkout -- filename
For a single file or
git checkout .
git clone email@example.com:josdem/git-workshop.git