Why command line Git?
There are several GUI tools, such as Tower and Github Desktop available for Git and it has basic support in Xcode, Visual Studio, and Android Studio, so why would you want to use Git on the command line? Git is very powerful and the more you understand it, the easier it is to use. Additionally, if you ever find yourself working on code in terminal or remotely via ssh, it might be your only option. A basic understanding of command line Git doesn’t preclude you from using GUI tools, but it just might help you out of a sticky situation down the line. Also, some of the GUI’s are open source and free, but many are on the expensive side. Command line Git is always free.
This guide assumes you are using terminal on Linux or macOS or the Git Bash prompt on Windows.
Download Git for your platform from https://git-scm.com/ and install.
Do this in the terminal before anything else!
Set up your username with
git config --global user.name = "First Last"
Set up your email with
git config --global user.email = "firstname.lastname@example.org"
A .gitignore file allows you to filter files that you don’t want added to your repository. Some resources to create a .gitignore file are:
Place the .gitignore file in the root directory of your Git repository. It is the same directory that you type
git init and has the hidden .git directory. If you type
ls -al you will see hidden directories in the directory list.
git init : Start a new Git repository
git status : Get the status of your Git repository (Run this often)
git add <filename> : Add a file to version control
git commit -m "<message>" : Commit using message
git add '*.txt' : Add all .txt files in the directory
git log : See a log of all commits
git remote add origin https://github.com/<username>/<project_name>.git : Add a remote repository
git push -u origin master : Push the local commits to the remote repository.
git pull origin master : Pull remote changes to local repository
git diff HEAD : See what has changed since the last commit
git diff --staged : See the git differences that are staged (ready to commit)
git reset <filename> : Remove a file from staged
git checkout -- <filename>: Get rid of all changes for since last commit
git branch <branchname> : Create a branch called
git checkout <branchname> : Switch to branch called
Branch and checkout can be combined with
git checkout -b <branchname>
git rm <filename> : remove the file AND stage the removal
Once all changes are made in your branch and you are satisfied, merge the changes back into the master.
git checkout master
git merge <branchname>
If you want to delete a branch, you can type
git branch -d <branchname>.
Don’t forget to push your code. When you are on master, type
A Very Basic Git Workflow Using Existing Code
- Change directory to the root of your source code
git initto create the repository
git statusto get the status of the repository. You should see a listing of Untracked files
- Create a
.gitignorefile. Here’s a really basic one for macOS from https://www.gitignore.io:
# Created by https://www.gitignore.io/api/macos ### macOS ### *.DS_Store .AppleDouble .LSOverride # Icon must end with two \r Icon # Thumbnails ._* # Files that might appear in the root of a volume .DocumentRevisions-V100 .fseventsd .Spotlight-V100 .TemporaryItems .Trashes .VolumeIcon.icns .com.apple.timemachine.donotpresent # Directories potentially created on remote AFP share .AppleDB .AppleDesktop Network Trash Folder Temporary Items .apdisk # End of https://www.gitignore.io/api/macos
- Add all files (excluding those from .gitignore) with
git add .
- Commit the files with
git commit -m "Initial Commit". The
-mtells git to use the following string as the commit message. If you omit this, you will get a vim screen prompting you for a commit message. Your commit will not be successful if you do not save the commit message in vim. To do that, type
ito enter insertion mode. Type your commit message at the top of the file (do not worry about the rest of the message). Once you are done with the message, hit
esconce to exit insertion mode. To save and quit, type
:wq(write and quit). If you want to simply exit without committing, type
:q!. Vim will exit without saving and git will abort the commit.
- Type ‘git status’ once again and verify that you see “nothing to commit, working tree clean”.
- It is best to work on branches and then merge them with the master branch after you are satisfied with your changes. That way, if all else fails, you can revert back to your last commit where the code was working. To create a new branch, type
git checkout -b "new-feature". You should see “Switched to a new branch ‘new-feature'”.
- Make some changes to your source code and then type
git status. You should see the file(s) you worked on now say “modified”.
- Either add the files individually with
git add <filename>or add all the modified files at once with
git add .
- Commit the files with
git commit -m "updated files"(your commit messages should be more descriptive that this, but it’s just an example). Now you can type
git statusto verify the commit happened. You can also type
git logto see the history of your commits with the most recent commit appearing at the top of the list.
- At this point, you can switch back to the master branch and see that your code changes in your new branch aren’t reflected there. Type
git checkout masterand then take a look at one of the files you changed. Don’t worry, you haven’t lost anything. Switch back to your new-feature branch using
git checkout new-featureand you’ll see your changes are back.
- To merge the new-feature branch with your master branch, switch to master with
git checkout masterand merge the new-feature branch with
git merge new-feature. Now all the changes you made in your new-featurebranch are reflected in your master branch.
- Repeat this step as you go along.
Sometimes you might accidentally make some changes, say to the master branch, before checking out a new branch and you get “error: Your local changes to the following files would be overwritten by checkout:” when you try to switch to another branch or create a new one. You have a few options if this happens.
- Commit the files using add and commit. You can take a shortcut and quickly commit all the changes in one command with
git commit -am "Updated files"or do it in two steps like we did previously. Once the files are committed, you can checkout a new branch.
- You can delete all the changes using
git reset HEAD. This will throw away any changes that you have made since the last commit. Use this with caution. It will not ask if you are sure.
- Use stash. Stash is a temporary stack that you can store changes on and then apply them later after you switch branches. To use stash do the following:
git checkout new-feature
git stash popto apply the stashed changes to the new branch.
If you want to see what has changed between your current branch and the master (or any other branch for that matter), you can type
git diff master. This will produce a diff file, which is a standard way of viewing insertions and deletions. If the file is long and you want to use a text editor to browse it, just type
git diff master > changes.txt. You can use any filename you want instead of changes.txt, but the diff will be saved to a file that you can then open in another text editor to view.
What about Github, Bitbucket, or Gitlab?
Each of these services provide a remote repository where you can store your code in the cloud and retrieve it later or share it with other people. Functionally, each works the same for git and uses standard Git commands. The main difference is the pricing structure and how you plan to use them, but each has a free option for personal use.
Rather than re-invent the wheel, I encourage you to visit their websites and explore their documentation. Each has excellent guides on how to set your project up on their server securely.
They also have an excellent Git Cheat Sheet that you can download and print for quick reference.
Sign up at https://bitbucket.org/ and then visit https://www.atlassian.com/git/tutorials/learn-git-with-bitbucket-cloud
You can also download and print Bitbucket’s Git Cheat Sheet
Gitlab can be found at https://about.gitlab.com/
If you decide you want to host your own git server, check out their Community Edition Omnibus Package