I have been trying to wrap my head around git for the past month, but have never had a solid chunk of time where I could just sit down and learn. Well, it just so happens that I had some time this past weekend. I found some fantastic YouTube videos, which I packaged into a Git Tutorial Playlist on my YouTube channel. At the risk of forgetting everything I have learned by tomorrow, here are some of the more common commands that I encountered today.
Below are the commands I find myself using at the moment. This will be a dynamic list as my quest to master Git continues.
git remote add origin firstname.lastname@example.org:username/repo.git
- By adding a remote origin on github, you are telling your local git repository that github is in control and holding all the files. This command is made from the machine where the project/repo originates.
git clone email@example.com:username/repo.git
- By cloning a repository you are copying (cloning) all of the files from GitHub and telling Git to start tracking revisions (commits) on your local machine. This command is done on the other machines that will be collaborating on the project.
- The aptly named .gitignore file is used to exclude certain files, file types, and/or directories from being tracked by Git. Make sure this file is placed in the root directory of the repo on your local machine. Exclusions are each listed on a separate line.
Saving & Retrieving Files
git pull origin master
- Whenever you start working on a project tracked by Git, as well as prior to pushing any code to GitHub, you should always pull the latest code from GitHub. That way you are always working on the freshest code.
git add [file.ext]
- This command adds a file to staging, which must occur prior to committing the file to your local Git repository. The file must be committed to your local Git repository prior to being pushed to your remote master repository on GitHub. Lastly, by replacing [file.ext] with “.”, you are telling Git to add all the files in the directory to staging (except those excluded through .gitignore).
git commit -m ""
- This command commits files to your local Git repository. It creates a “save state” in which you are telling Git to take a snapshot of your project at that specific moment in time. It is best practice to pass a message along with each commit. The message is wrapped in quotes after the -m.
git commit -am ""
- This shorcut is the equivalent of performing a git add + git commit -m “” command.
git push origin master
- This command pushes the same files that you committed to staging in the previous step in to the “master” branch of the repository. Note: if you had not specified “origin master”, git would have
git reset HEAD [file.ext]
- This command unstages the specified file. It is used when a file is staged, prior to being committed in to your Git repository. If the file has already been added to your Git repository, you will need to use the next command to remove it from Git
git rm -h
- This command will show you all of the options for removing files from Git.
git rm --staging [file.ext]
- This command will remove the specified file from your Git staging directory. Following this, you will need to commit this change to your local repository and push the change to your remote GitHub repository.
git checkout -- [file.ext]
- When you make changes to a file in your working directory that you dicide not to commit, the checkout command will retrieve the specified file from the repository and overwrite the same file in your working directory. The “–” signifies that you want to stay on the same branch. We put the “–” in place so that git knows that we are specifying a file to checkout, not a branch.
git commit --ammend -m "[updated message]"
- Changes can only be made to the HEAD (last commit on the branch). This is because you cannot modify commits in the repository that are referenced by other commits. Since the HEAD is by default pointed to the most recent commit, no commits are yet referencing it. The “ammend” command tells git that you want to revise/edit/update the last commit (maybe you forgot to add something to the commit). The message that you pass along with the “ammend” command will overwrite the current mesage for the commit.
Monitoring Code Changes
- This is probably the most commonly used command in Git. It will tell you exactly what is going on with all of your files – what files have been changed, what needs to be added to staging, what needs to be commited to your repository, and more.
- On *every Git command (I think), you can use the “-h” to show help text for the command. Help text typically includes all of the Command’s options, along with a breif explanation.
- This command will display a list of branches for your Git repository. The branch with the “*” next to it is the active branch.
- This command shows you the history of all your prior commits. The log includes who did what and when they did it.
- This command shows the abridged history of your prior commits. It similar to the previous command, except that the date, author, and complete hash are not shown. It is a useful command for displaying historical commit messages.
- This command shows you what was changed in the most recent commit.
- This command launches a wysiwyg ecitor that allows you to navigate through your commits and compare code changes among those commits.
- This command launches a tool that is used to resolve merge conflicts. It allows you to compare two versions of the conflicted file and make the necessary changes before you decide which version to save. Merge conflicts occur when two developers are working on the same file at the same time.
Commonly Used Terminal Commands
- Navigate to the root directory.
- Navigate to the specified directory.
- Navigate up one directory.
cd ../ ../
- Navigate up two directories.
- List all files in the current directory.
- List all files in the current directory, including hidden files.
mv [directory] [directory]
- Move the specified directory to the next specified directory.
- Clear all commands in the terminal screen.