Git Commands

  1. If you make a change to your software, you can “commit” it. If you are working on a separate feature, you can create what is called a branch. To add a file to be committed, you use the add command. If you want a feature branch to be put into the master branch (the main one), you have to merge. The diff command in git shows you the differences between things. The checkout command lets you change to a different branch. clone lets you download a remote repository.
  2. The config command lets you change configurations for your git installation. If you want to see who did something, you can use the blame command. The pull command will pull changes from a repository. push will push the code you made to the repo, but only after you add and commit. There are many other useful git commands, such as squash, rebase, init, log, show, and ls-files. There is a special file in git called .gitignore which will ignore any such files and not put them into your repository, which is important if you have a config file with login info and your repo is public.
  1. git –version – show the version of git you have installed.
  2. git config – set up your git settings on your machine.
  3. git init – create a new local repo.
  4. git clone – copy a repo that already exists, such as one from GitHub. I like to make a new repo on GitHub, then git clone. I don’t git init much for making new projects.

git pull – pull down the latest changes from the repository, such as if other contributors made their own commits/branches/merges.

  1. git add – add a changed file to be in your upcoming commit. Example: git add index.html
  2. git add . – add all in the current directory.
  3. git add -A – add all
  1. .gitignore – One little tip I have is that I use .gitignore to make sure to ignore .DS_Store, which is a common annoyance on macOS. .gitignore is a file that will exclude whatever you put in it. Another thing a .gitignore file is useful for is if you have a config file that contains login credentials. You don’t want those to be made public on GitHub. Also, files that start with a . are usually hidden by default.
  1. git status – view the status, such as if you have tracked files that are ready to be committed/pushed.
  2. git commit – a commit is a mini version/release of a program. You make small changes, then make a commit to save those changes. If you break your software, like if something crashes because of code you changed, you can always go back to a previous commit. A commit prepares you for pushing the changes to your remote git repository, like on GitHub.

I used to make very few git commits, and they were always massive updates. Now, I commit smaller and more frequent changes. It works out better this way. If you make an unwanted change and have to revert to a previous commit, you won’t lose as much progress this way.

  1. git commit -m “commit message goes here” – commit with a commit message that briefly describes what you did.
  2. git push – send your changes to the git server, such as GitHub.
  3. git checkout – lets you change to a different branch.
  4. git branch – create a new branch with a given name, like git branch coolbranch. Branches are used for working on separate features. When you’re working on a feature, you do it on a feature branch. When you’re done, you merge it back into the master branch, which is the main one. Do not work directly on the master branch, as it is sacrosanct in the realm of git.
  5. git merge – combine the changes of one branch into another branch, such as master.
  1. master – the master branch in git is very important, and you’re never supposed to work on it directly. master branches are like the git equivalent to production environments.

git reset –hard HEAD – reset to latest commit (HEAD). Do this if you’ve messed up your code and want to revert back to a working state. This is one of the best features of version control. Keep in mind: this means you will lose your changes you’ve made since your last commit. That’s why it’s important to commit often.

git log –abbrev-commit – view a log of commits, but with abbreviated commit SHA1 hashes. This is useful for the command listed after this one.

git revert 2b91e33 – revert to a specific commit with the example hash of 2b91e33 . When you’re doing this, your hashes will be different. A related command is git reset –hard 2b91e33.

The simplest git workflow (once you’ve set up your GitHub credentials and also cloned a repo):

  1. Edit source code files, maybe recompile stuff.
  2. Use git add filename to add an edited file to be tracked to be committed in the next commit.
  3. Use git status to see what you have and haven’t added to be tracked in the next commit.
  4. Use git commit -m “added [some feature]” to commit the changes that have been made.
  5. Use git push to push the changes the repository.

If you are writing code by yourself, rather than in a team, the above commands are what you’re going to be doing most of the time. You can also introduce more advanced topics into your workflow, such as branching, merging, testing, and so on. But for beginners, the above 5 commands are all you need to get started.

  1. Want to do more on GitHub, but don’t know where to start?
  2. Here are some good things for beginners to do in order to get more active on GitHub:
  3. First contributions:
  4. Hacktoberfest in October:
  5. Advent of Code in December:
  6. LeetCode (maybe make a repo and do one problem per day):
  7. Project Euler (make a Project Euler repo and do one problem per day):

← Previous | Next →

GitHub/Project Management Topic List

Main Topic List

Leave a Reply

Your email address will not be published. Required fields are marked *