Altijd in beweging...

Technische ontwikkeling staat nooit stil

  • git

Git Basics II

July 10, 2019 source control
Main Photo by Yancy Min on Unsplash

What are the main commands I use when working with Git?

Clone a repository

When you clone a repository the Working directory and Local repository get updated. The default name for remote is origin.

To clone a Remote repository to a folder with the same name as the repository ("sample" in this case):

git clone git@github.com:tm/sample.git

To get the local environment setup in a different folder, add the folder as a parameter

git clone git@github.com:tm/sample.git <other-folder>

Commit changes

When you want to update the Remote repository with your changes, these changes move through all parts of your local environment and are pushed from the Local repository to the Remote repository.

To make the files git-aware use the add command. With the . or -A all filechanges are taken into account. With -u only modified and deleted files are considered (not new files).

git add .
git add -A

git add -u

Add staged files to the Local repository. When the commit is given without a commit-message the default Git-editor will be opened to enter a message.

git commit
git commit -m "Fix typo in file"

The normal flow is staging files to mark them for the next commit. With the -a flag you can skip staging and commit all changed files.

git commit -a -m "Fix another typo in file"

To correct the previous commit or commit-message use the --amend flag. It will completely replace the previous commit.

git commit --amend

To transfer the commits to the Remote repository use the push command:

git push
git push <remote> <branch>

Get updates from a repository

When you want to get updates from the Remote repository you fetch or pull the Remote repository. When you fetch, only the Local repository gets updated. pull also updates the Working directory by merging the changes.

git fetch
git fetch --prune
git fetch <remote>
git pull

Note: by default a pull does a merge into the Working directory. A rebase is also possible by adding the --rebase flag. This will keep the commit history cleaner. Since rebase isn't explained in this article I didn't mention it here. Check out the advanced article to learn about rebase.

Several other common commands

Status
Get information about the current state of your local environment. Optionally add the --short flag to get a compact summary.

git status
git status --short

Compare
Get information about the changes in files.

Compare the Working directory to the Index.

git diff

Compare the Index to the Local repository. The staged and cached flag have the same result.

git diff --staged
git diff --cached

Remove a file
Remove a file from Git. The file will be deleted from the Working directory and Index.

git rm <file>

When you want to remove a file that has been modified or staged you can use the -f flag, but be sure to know that you want your changes to be deleted.

git rm -f <file>

Rename a file
Rename a file and add it to the Index. Under the hood this command will remove the <from> file Git (git rm) and add the <to> file to Index.

git mv <from> <to>

Remotes
show the remotes that are available.

git remote

Log
Get information about the commit history where the commits are ordered from newest to oldest. Some options are mentioned below but there are a lot more so check the documentation on this.

git log
git log --patch
git log -<number of commits>
git log --graph
git log --pretty=oneline
git log --no-merges
Previous article Git Basics I
Next article Git Basics III