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

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

git clone <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 file changes 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.

Several other common commands

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

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 flags 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>

show the remotes that are available.

git remote

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