DEV Community

Kelvin
Kelvin

Posted on • Edited on

GIT BEGINNERS GUIDE.

Version control

version control is a system that records changes to files over time making it easier to track code, restore and collaborate safely.

Git

Git is a distributed version control system used for code collaboration, code restoration, tracking code changes and who made them.

Git commands

git --version
Enter fullscreen mode Exit fullscreen mode


git --version checks whether git is installed on your computer and what version is installed.

git config --user.name"yourname"
Enter fullscreen mode Exit fullscreen mode


git config --user.name"yourname" sets the author's name linked to the commits.

git config --user.email"youremail"
Enter fullscreen mode Exit fullscreen mode

git config --user.email"youremail" sets the email address linked to your commits.

git config --list
Enter fullscreen mode Exit fullscreen mode


git config --list shows all git configuration settings currently applied to the system.

ls ~/.ssh
Enter fullscreen mode Exit fullscreen mode

ls ~/.ssh looks for SSH folder in the home folder.

ssh-keygen -t ed25519 -C"youremail"
Enter fullscreen mode Exit fullscreen mode

--
ss-keygen -t ed25519 -C"youremail" generates private and public SSH keys.

eval "$(ssh-agent -s)"
Enter fullscreen mode Exit fullscreen mode


eval "$(ssh-agent -s)" starts the ssh agentand sets the necessary environment variables in the current terminal session.

--unset
Enter fullscreen mode Exit fullscreen mode

--unset is used to remove a setting.

ssh-add ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

ssh-add ~/.ssh/id_ed25519 allows git to use your key automatically without asking for the passphrase every time.

git init
Enter fullscreen mode Exit fullscreen mode

git init creates a hidden .git folder that git uses to track changes.

git add .
Enter fullscreen mode Exit fullscreen mode


git add . tells git to stage all changes in the current directory and its sub directories so they are ready to be committed.

Push commands.

git push
Enter fullscreen mode Exit fullscreen mode

git push is used to send local commits to a remote repository.

git push -u
Enter fullscreen mode Exit fullscreen mode

git push -u is used to push the current branch and set its tracking branch.

git push --all
Enter fullscreen mode Exit fullscreen mode

git push --all pushes all the local branches to the remote directory.

git push --tags
Enter fullscreen mode Exit fullscreen mode

git push --tags pushes all the local git tags to the remote repository.

git push --force
Enter fullscreen mode Exit fullscreen mode

git push --force forces the local branch to overwrite the local branch.

pull commands.

git pull
Enter fullscreen mode Exit fullscreen mode

git pull gets changes from a remote repository and merges them into the current branch.

git pull origin main
Enter fullscreen mode Exit fullscreen mode

git pull origin main fetches the latest changes from the remote origin and merges the main branch into the current local branch.

git pull --rebase
Enter fullscreen mode Exit fullscreen mode

git pull --rebase updates the branch by replaying the local commits on top of the latest remote commit.

git fetch
Enter fullscreen mode Exit fullscreen mode

git fetch downloads the latest changes from a remote repository without modifying the local branches.

git fetch --all
Enter fullscreen mode Exit fullscreen mode

git fetch --all fetches updates from all remotes without changing the local branches.

git pull --ff
Enter fullscreen mode Exit fullscreen mode

git pull --ff tells git to update the branch using a fast forward merge only.

git fetch
Enter fullscreen mode Exit fullscreen mode

git fetch downloads the latest changes from a remote repository without changing the local branches.

git fetch --all
Enter fullscreen mode Exit fullscreen mode

git fetch -- all fetches updates from all every remote without changing the local branches.

Creating directory commands

mkdir
Enter fullscreen mode Exit fullscreen mode

mkdir is a command used to create a new directory.

mkdir -m
Enter fullscreen mode Exit fullscreen mode

mkdir -m is used to create a directory and set its permission at the same time.

mkdir -p
Enter fullscreen mode Exit fullscreen mode

mkdir -p creates a directory and any missing parent directories.

cd
Enter fullscreen mode Exit fullscreen mode

cd (change directory) is used to navigate into a git repository before running git commands.

cd ~
Enter fullscreen mode Exit fullscreen mode

cd ~ moves you to the home folder.

cd -
Enter fullscreen mode Exit fullscreen mode

cd - takes you back to the previous directory you were in.

cd /
Enter fullscreen mode Exit fullscreen mode

cd / command moves you to the root directory of the file system.

cd "my folder"
Enter fullscreen mode Exit fullscreen mode

cd "my folder" is used to change into a directory whose name contains spaces.

Tracking changes

git status 
Enter fullscreen mode Exit fullscreen mode

git status shows the current state if the working directory and staging area.

git diff
Enter fullscreen mode Exit fullscreen mode

git diff is used to see the exact changes between files, commits, branches or stages in git.

git log
Enter fullscreen mode Exit fullscreen mode

git log is used to view the commit history of a git repository. what was committed by whom and when.

git blame
Enter fullscreen mode Exit fullscreen mode

git blame is used to see who last modified each line of a file, when, and in which commit.

git show
Enter fullscreen mode Exit fullscreen mode

git show is used to display details of a git object. It lets you see what changed who changed it and when.

Top comments (0)