Say you and your ferret loving friend want to create a website about ferrets. You know how important version control is, but neither of you have any experience with git. These are some of the most important git commands you need to know to bring your project to a good ending, and share your knowledge of ferrets with the rest of the world.
We will start off with a folder, containing only one file: ferrets.txt.
To initialize a git repository in this folder, simply use the command $ git init
To check the status of your current branch, you use $ git status
As we can see, we are on the master branch, and this is our initial commit. Also, git notifies us that we have untracked files. To add a file use $ git add <file>
We can now store the changes in a commit and add a message, using $ git commit -m “our message”
When making changes to your project it is good use to do this in a separate branch and leave the master branch alone. To create and go to a new branch use $ git checkout -b Branch-Name
The -b flag tells Git it needs to create a new branch. You do not use this flag to checkout to an existing branch. Now let’s add a picture of a ferret and check the status.
We can add the picture using $ git add ferret.jpg, but when we have hundreds of pictures of ferrets, we do not want to add them all by hand. To add all untracked files, use:
Be aware that this may also include unwanted files. If there’s any files we do not want to include in our commit, we can remove them with $ git commit — <file>
Now create a commit and add a descriptive message. You can now checkout to master, and apply your changes. Now that we do not need our Ferret-Picture branch anymore, we can safely delete it using $ git branch Branch-Name -D
Now let’s add some ferret facts to our text file. We checkout to a new branch, add some ferret facts to ferrets.txt, add the file, and finally create a commit. Oh noes, we forgot to add one of the ferret facts! What we can do is create a new commit. But that will leave us with an extra commit with only one change. A clean way to resolve this is to amend your last commit: $ git commit –amend
By using –no-edit we do not need to write a new commit message, and Git will use the last commit message. To check the changes between files on different branches, use $ git diff Branch1 Branch2 or ad –name-status to only display the file names:
To clean untracked and unstaged files and remove empty directories, use $ git clean -fd
Now, let’s assume your friend has set up a remote repository. You can locally access repository by cloning it, using $ git clone git@gitserver:some/path/ferrets.git
You can pull all changes to a branch to keep it up to date by using $ git pull
As we can see, the file ferrets.txt was edited, and a new file pull.txt was added. To view the commit history of a branch, checkout the the branch and use $ git log
After a pull, we also have local access to remote branches. We can check out to a branch, commit changes, and then push the commit using $ git push
If we create a local branch, and we want to add it to the remote repository, use $ git push –set-upstream origin Branch-Name