Common Git Commands

Git can be a pejorative for an unpleasant person. Make your experience with Git a nice one.

Image for post
Image for post
A visual representation of the Git process

There are still developers who don’t use version control! One of the best things about being a developer is being so far ahead of other workers who have to battle against version control in Word (or similar), and live light-years behind the cooperation tools that programmers get to use each and every day.

Prerequisites:

  • None

Terminology

Git: Git is a distributed version-control system for tracking changes in source code during software development

Concepts

Repositories — A repository is a file location where you store all of the related files for the current project. A local repository resides on your system, a remote repository (generally) lies on a remote machine. A code project can consist of multiple repositories across multiple accounts, but commonly is a repository from a single account. Repositories (also known as Repos) are recommended to be kept under 1GB.

Tracking — Git takes snapshots of the whole state of a project. A revision in the repository relates to the state of the whole project, rather than single files.

Staging area — often referred to as the index (as described above)

Commit — similar to a save or a snapshot of the current project. You set a message about the changes performed, and saves a revision of the code that you can revert to anytime in one click. Commits allow you to go back to this (named) state of your project at any point.

Working tree (Working Directory)— a working tree is a directory in your file system that is associated with a repository

The typical workflow for git:

Image for post
Image for post
  • make changes tot he working directory
  • git add filename (stage a file, to the Git index)
  • git commit filename (commit to the repository)

General commands

git config

A good start is to list your current configuration

git config-list

Which can also be updated field by field with

git config --global user.name "firstname secondname"

Another great configuration to manage is your choice of merge tool to resolve conflicts, which can then be called later through git mergetool.

git init

git init

which creates a .git folder in your current directory.

It is possible to delete this folder if you wish to *get rid* of the initialisation of git for that particular folder.

git remote add

git remote add

to list all current remote repositories:

git remote -v

git rm

git rm file
git rm file.txt // will delete file.txt

git rm -r (directories)

git rm -r folder

git fetch

git fetch

git checkout

git checkout

git fetch && git checkout

git fetch && git checkout <branch-name>

git push

git push

git checkout

git checkout <:branch:>

git stash

A good case for using this is when you want to switch branches, but don’t want to commit what you’ve been working on yet.

git stash

You can see which stashes you’ve stored by using:

git stash list

and to restore the stash you can run the command

git stash apply

git fetch

git fetch

Because it does not integrate changes you are guarenteed (well, almost) that this will not screw anything up, so you can perform a fetch as many times as you like.

git clone

git clone

You don’t have to init first, as this is already done by the clone command (and in fact will create an error when you do so). You can think of clone as a combination of init, remote add, fetch and checkout.

git commit

git commit -m "my first working commit"

git commit and stage

git commit -am "my first working commit"

git reset

git reset <:mode:> <:COMMIT:>

git status

git status

Branch commands

git branch

git branch

git checkout

git checkout <branchname>

git branch -d

git branch -d

git push origin

git push origin -d <branchname>

You can also push all branches to the remote repository.

git push origin -all origin

git push origin

git push origin :<branchname>

Update from the remote repository

git diff

git diff

git pull

git push

git merge

Typically you merge from a branch into the master (or from a feature branch into the developer branch). That is, you checkout the main branch and then merge into it from your branch.

git checkout master #at this point you MUST have committed your changes to your branch, so you do not risk losing anything 
git merge yourBranch

A merge can be undone (so you aren’t going to break anything) simply through:

git merge --abort

git add

git add .

also the most powerful feature that you’re not using yet that allows you to change parts of a changed file instead of the entire file.

git add -p

Tags

git tag

git tag 1.0.0 <commitID>

Undo changes to the local repository

git checkout

git checkout

git fetch origin

git fetch origingit reset --hard origin.master

Search

git checkout

git grep "searchterm"

Best practices

  • never nest repositories
  • always use git status before typing another command
  • never use git commit -m

Want indepth tutorials? Just use this link: Atlassian Git Tutorials

Note:

Want to get in contact? Use the link here:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store