Git basic setup and commands, create, initialize and use git/guthub repository to version control your source code !
1 – Introduction
Git is version or source control system that tracks changes of your source code files and allow you to versioning them. Git is an application that you download and install from here
Git is a distributed version control system – which just means that you will have a local repository (git directory) which will contains a local copy of your code and a remote repository that will be shared with other users, that’s why changes to code are checked-in in two steps (commit, push !).
A remote repository could be hosted by online providers like Github or on-premise option (Github will be used in all examples of this tutorial !)
1.1 – Setup
Once Git application was installed, you’ll need to add some configurations like your profile information, so that you’ll be recongnized when you interact with remote Git hosts like Github.
Git comes with a tool called git config that allows get and set configurations. There is multiple levels scoped configurations (global, system, local, etc. See more here.) but to keep things simple lets set the global one.
- First you can check what configurations are already in place
git config --list [--global/local/system] - To configure your identity (if not already done by your IDE like VS or VS Code) run those two scripts :
git config --global user.name "John Doe"andgit config --global user.email johndoe@example.com
or edit the config file directlygit config --global --edit
this will set your profile name and email globally this means that you will use that information for all interactions from this machine. You can omit--globaloption if you need to target only a specific project/repository.
1.2 – Repository
Git repository
Git repository is a local directory thats will contains your code but also a git subfolder (named …/.git) used by git system to do it’s work (delete this folder means that his parent folder is no longer a git repo) !
To create manually a git repo you need to create a directory and/or cd to it and type git init. Note that repo is not conneced yet to a remote one, this can be done git remote add origin https://github.com/user/repo.git
When you clone a remote repository, automatically a local one will be initialized and will be connected to that remote repo ! Use git clone https://github.com/user/repo.git
Before creating the first commit, a branch, locally is not created yet. Create a first commit and you’ll see your root branch with its default name, you can then rename (git branch -m master main) it before pushing it.
Nota : If you’re using SSH url like git@github.com:username/repo_name.git, you may need further configurations.
Github repository
Github offer free code online hosting, you can create private and public repositories easly.
You need to create a Github account and start by creating a repo that will host your code ! (git remote add origin need a remote repo created 😉)
2 – Useful git commands
Exit git list/log
SHIFT + q
Show unstaged changes in your working (local) branchgit status
Show commits history in your working (local) branchgit log
Stage : save, add or prepare the changes to be committed (‘.’ to add all changes)git add . or filename1.cs filename2.sql
Commit : save changes locally to your current (local) branch : git commit -m "comment"
Push : save changes to your (remote or origin) branch : git push -u [-f] [origin branch_name]
or if you push for the first time.git push --set-upstream origin branch_name
NB : use ‘ ‘ if the branch name contains unaccepted chars !
Get the latest version of your (remote or origin) branch : git pull [origin branch_name]
Change branch (locally) :git checkout branch_name
NB : Also means remotely since every local branch is behind an origin or remote branch.
Change to a new branch (locally) :git pull (update a parent branch)
or for remote git fetch origin (and git branch -v -a to see available branches)git checkout -b my_branch [origin/][parent_branch]
Save uncommited changes, get them back or move them (locally) : (use -u to include newly created files also called untracked) git stash [-u]
– You can name a stash : git stash save -u MyStash
[] (this normally used to stage changes but also to add files if needed)git add .- change branch, pull or any action
[git stash list] (show different stashs in diffent branches of the repository)git stash pop
Delete local branch :git branch -D branch_name
Merge your branch (source) to another one :
– [Using remote branche for source and] passing by local branch for targetgit push (push your changes you would to merge)git checkout target_branchgit fetch (usually needed)git merge [--no-ff] origin/source_branch (not origin source_branch)git push (push newly merged changes)
– [Or using local branche for source]git checkout target_branchgit merge [--no-ff] source_branchgit push origin target_branch[git push origin my_branch]
– In case you can’t merge using the target‘s local branch (e.g. merge need pull request)
Rebase your branch from another one :
– Using remote branches
git rebase origin/source_branche
– Using local branchesgit checkout source_branchgit pullgit checkout my_branchgit rebase source_branch
– To leave terminal and resolve conflicts : “Escape” “:” “w” “q”
– To resume rebase : git rebase --continue (or –abort, –skip)
NB : Unlike rebase, merge command creates an additional commit and don’t keep history and it’s not recommended to reuse a merged branch. Use rebase to update your branch with changes in a master branch (changes from master to slave) and use merge to update a master branch with your changes once work is done (pull request, etc.).
Add changes to commitgit add <file added or modified>git commit --amend
– To leave teminal : “Escape” “:” “w” “q”git commit --amend --no-edit (witout changing message)git commit --amend -m "Your new commit message"
Apply a commit from another branch to current branchgit cherry-pick <commit_hash>
Get list of remote and local branches :git fetch to update remote-tracking branchesgit branch_name -a
Undo a saved commit to local branchgit reset HEAD > ~1 or git reset HEAD~1
– Deleting changesgit reset --hard HEAD
git reset --hard HEAD~1
– Undo reset –hardgit refloggit reset --hard <id>
Undo a pushed commit to remote (origin) branchgit revert HEAD~1..HEAD
git revert commit_code
Show remotes repos/branchs and thier shortnames
By default origin is shortname of the saved remote repo
If you have more than one remote, the command below lists them allgit remote -v See more heregitk See more here
