BearBlogtech

Git Basics

19 janvier 2025

The basic command to start with git.


create a new branch

git branch newbranch
git checkout -b newbranch (Create a new branch and immediatly swith to this branch)

add a file to the repo

git add .
git add namefile
git commit -am "commit message" (commit and add file at the same time)

get all changes from another repo or a local branch

git pull


keep a trace of all commit you made

git log


get back to the commit you want, you can get the list of your commit with git log

git reset --hard


get back my local repo at the state of the master repo

git reset --hard origin/master


to clone the remote repo into local, can be made with https or ssh

git clone git@github.com:username/repo.git
git clone https://github.com/projectname/projectname.git


display the status of your working tree

git status


switch branch

git checkout branchname


merge

pay attention to your current branch
git merge branchname
this is going to merge the current branch with the branch branchname


Somes examples

user@srv/Test_Git_learn$ git branch
* master
style
test-branch
user@srv/Test_Git_learn$

user@srv/Test_Git_learn$ git checkout style
Switched to branch 'style'
user@srv/Test_Git_learn$ user@srv/Test_Git_learn$ user@srv/Test_Git_learn$ ls
hello.txt
user@srv/Test_Git_learn$

user@srv/Test_Git_learn$ git status
On branch style
nothing to commit, working tree clean
user@srv/Test_Git_learn$

user@srv/Test_Git_learn$ echo "wrtie change in file from style branch" >> hello.txt
user@srv/Test_Git_learn$ cat hello.txt
Hello wolrd 2
Hello wolrd 3
Hello wolrd 5 because 4 has been restored
write change in file from style branch
user@srv/Test_Git_learn$

user@srv/Test_Git_learn$ git commit -am "commit from style branch"
[style 997077d] commit from style branch
1 file changed, 1 insertion(+)
user@srv/Test_Git_learn$

user@srv/Test_Git_learn$ git push
fatal: The current branch style has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin style
user@srv/Test_Git_learn$ git status
On branch style
nothing to commit, working tree clean
user@srv/Test_Git_learn$

user@srv/Test_Git_learn$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
user@srv/Test_Git_learn$

user@srv/Test_Git_learn$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
user@srv/Test_Git_learn$

user@srv/Test_Git_learn$ git merge style
Updating 3a9c410..997077d
Fast-forward
hello.txt | 1 +
1 file changed, 1 insertion(+)
user@srv/Test_Git_learn$

user@srv/Test_Git_learn$ git push
git@10.10.10.204's password:
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 24 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 322 bytes | 322.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To 10.10.10.204:/home/git/TEST.git
3a9c410..997077d master -> master
user@srv/Test_Git_learn$


"Branching"

working on different parts of the repo at the same time

HAED = where you are actually working rigth now
you can change the head and working on a different branch
by switching on a different branch

for an exisitng projet with a master branch in the directory where you store the file like this


working on new features without disrupting the master branch version of the code