The GIT command I used often
I have written another post on basic and advanced commands, but I decided to write another post on the GIT
command I used often. Well, GIT
has so many commands, and some of the commands actually work the same but have different purposes or functions.
I have decided to write a simple one, which 99% of the people will use on pushing your code to GitHub
. Note that I will not cover complicated commands that some people might not know. If you are interested in other commands, you can refer to my other post.
I will cover this section :
- Basic command
- Branch-check and create local and remote repository
- Debug command:
log HEAD~N
- reset and restore
- merging branching
Prerequisite:
Please register github account, and install git related tool. I’m not going to cover in this section.
Basic command
1.git init
git init
means initial this directory‘s version control, so it will generate .git
directory, .git
is a hidden file.
2. git add
Add a file or code in this directory. You need to stage this directory, so I will use the git add .
to stage this directory. You can also stage-specific files using this command: git add <filename>
.
3. git commit
After you use the git add command, then we need to commit the file into the local repository with -m
the option, which stands for a message, git commit -m “message”
.
git push
Right now we need to push all the files into remote repository or github. Remote repository basely means to push our local file into the server which is Github.
Before git push to the Github server, we need to add remote URL address, else we are not able to push to the server. So we need to add a new repository like below
- add your remote repository
git remote add <remote repoistory name> <url>
- add remote branch and push to github
git push -u origin main
-u
is optional, if you add -u
option, it will record your remote repository as default, next time you want to push to the server you can just use the command without -u
, like this: git push
. I personally like to use it.
main
is the branch name, it will push the file to github server.
4. git remote
This command checks the local remote URL, you can delete it or edit the URL
- We can use this command to check or change
git remote -v
: see if your git remote URL is added or not
- delete or remote remote repository
git remote remove origin
- edit remote repository
git remote rename <oldname> <newname>
Branch- Local Repository
5. git branch (local)
check branch (local repository)
There are several commands I will use:
- check branch:
git branch
There are couples of options we can use:
-a
all branch include local and remote
-r
remote branch
-l
local branch which is default same asgit branch
create branch ONLY
- Create a new branch :
git branch <branchname>
Delete branch:
- delete branch:
git branch -d <branch name>
-d
flag merge branch
-D
flag not merge
git checkout and git switch — add and switch branch
We can use switch
or checkout
command. The switch
command is a new command, before we use the command checkout
. You can think as: git checkout = git pull + git switch
But anyway, you can decide to use either switch or checkout. Checkout
commands are not only used to add/switch branches but also allow us to change the head to a different commit id.
switch command
This is a command to add and switch branch
- add and switch to a new branch:
git switch -c <non_existing_branch>
checkout command:
- add and switch branches:
git checkout -b <non_existing_branch>
- switch specfic branch:
git checkout <exist-branch>
git branch (Remote Repository)
push the local branch to remote repository
So if we want to push our branch into a remote branch we have to use the below command:
command:
git push <remote repoistory nameorgin> <branch name>
or
git push — set-upstream <remote repository> <branch-name>
My local branch name is newbranch1
, so the command I will use like this: git push orgin newbranch1
pull remote branch to local working directory
So in the previous one, we learn how to create a branch and push it to the remote repository. What if you want to pull a remote branch to the local working directory or local repository?
So let's create a remote branch in GitHub server:
So we can check our local branch git branch -a
and you will not see you remote branch you create.
you can use this command git checkout origin/remote-branchName
, step as below:
- check remote branch
git branch -a
orgit branch -r
git pull origin
- git switch main
- check remote branch again
git branch -a
orgit branch -r
delete remote branch
git push origin --delete branchname
or shorter commandgit push <remote>:<branch>
synch local and remote branch:
fetch --prune <remote repoistory name>
Prune command: Clean Up Remote Branches in Git
If we delete the remote branch from GitHub server, but local working directory still shows the remote branch. In order to synch with update branch from GitHub need to use this command: git fetch — prune origin
.
If you don’t want to fetch
, instead, use remote
command:
git remote prune origin
git checkout: move commit hash
moving our commit message, to recover to specific hashtags. Please refer below picture to understand the HEAD. On the left one is when you commit the latest message, which we called this HEAD. If you want to move a commit message, there are many different commands. In this example, I will use the checkout command.
Remember we use checkout to create and switch branches, we can also use this command to move out HEAD.
- move HEAD to a specific hashtag :
git checkout <commit hashtag>
- restore to original HEAD:
git switch -
- move HEAD using HEAD~N:
git checkout HEAD~N
Merging branch
git merge:
Since we already learn to create branches, then you must need to know to merge different branches. There’re couples of merging flag option you should know:
git merge [--ff | --ff-only | --no-ff] <branch-name>
there are couples of merge which includeff
(fast-forward), and rebase.
--ff
fast-forward default (linear merge)
command:
git- merge -ff <branch-name>
The default merge uses--ff
which is fast-forward, you would ask me what’s fast-forward. So basely --ff
means simply moves the source branch with target branch pointer without creating an extra merge commit.
In the below we have a master branch (source branch) and Feature is our target branch, so the Feature branch merges with the Master branch which in this case is the C. C will merge with old master branch without new commit message.
However, if your master branch is ahead of the Feature branch like below:
From the above picture D(master) is ahead of C(feature), so you’re not allowed to use fast-forward, it will automatically help you change it to NO-FF
the method. But you still can force it using--ff-only
, but it will pop an error.
#merge force FF-only
$git merge --ff-only <branch-name>
- no fast-forward (Recursive merge)
•Create new commit
•No new commits on master
command:
git merge --no-ff
From above we know how to use fast-forward, we can also use no fast-forward. The differences between fast-forward and no fast-forward are:
FF: merge without a new commit , default.
no-FF: merge with a new commit
No-FF
allow you to see the history of merge(commit hash), but when you have too many developers commit, it will be messy and hard to read. To solve this issue, we can use rebase merge.
- rebase(linear merge)
Rebase will re-write and overwrite to original commit just like the below picture.
As you can see original C and after merge C will have a different commit ID, because it’s been overwritten.
Let me show you an example please refer below picture:
Please see the last commit hash ID has even changed. The original master commit hash is ce9a, after using rebase it overwrites the original commit, as you can see the hash is been changed to fb24.
Note: if you use rebase I hihly recomment use the log command with graph option: git log --oneline --graph
it is pretty useful and shows more clearly on merging.
git pull:
git pull
:git clone
+git merge
When do we use git pull? I use it when I push git from PC/laptop-A, and PC/laptop-B I will use the git pull to update my latest repository because PC/laptop-B will not have the latest code or file.
git clone:
git clone work the same as git init, the difference between both is git init
work on the local workplace, whereas git clone works remote repository download to local workplace.
git init
: create empty directory=>run git init=>github create repository on github server=>local side add remote reposotory =>push to server
git clone
: create repository on github server =>git clone to download from remote to local
Debug command
git log
git log command is been used often, especially when checking commit hash. So what is commit hash? When we commit our git into server , it will record the hash. The hash is pretty important which allow use to move our HEAD to specific.
You can think it as when you save your file, it will have a record, so it allow us to recover back in future. So let me show you what’s it means hashtag.
You can use this command:
- check the log, full description log:
git log
- show one line, only show the hashtag and commit message:
git log --oneline
git log --online HEAD~N
We have to first understand what is HEAD, HEAD~n
, N
means the last commits log. So let see the below picture, if you use git log --oneline HEAD
, it will list all logs. If you want the last(oldest) commit log, this is where you put N in it. For example git log --oneline HEAD~3
, show the last 3 commit messages.
git log --online --graph
This command will show your commit and branch in graph mode, which is useful to see your branch and merging flow.
Restore and checkout restore staging
revert : git checkout
and git restore
checkout:
git checkout <filename>
restore:-
git restore --staged <filename>
git checkout: git checkout is when you want to cancel the recent changes to files. This only works when you have not yet stage it. You can think it’s like a undo key we use in Office Word.
Working directory not staging:
git checkout -- <filename> revert back to workspace #only one file
git checkout -- . #multiply file
git checkout <commit id> #move head to specfic commit id, you mention before
git restore
Let us look at this picture to understand recover command from stage to unstage. In the below picture we edit a file and stage the file, but then you refuse to stage it. Maybe something is wrong with the file, and you just refuse to stage this version.
git restore --[staged|source] <filename>
--stage
:(Unstage files)Removes the file from the Staging Area, but leaves its actual modifications untouched.
git revert
The git revert command is only for reverting a commit to a previous commit. It’s going to Rollback changes you have committed before.
Reset command
git clean: remove untracked files
There are two flags:
f: force to delete or clear file
-n: will give you message which file will be cleaned.
#step 1
touch test.txt#step2 check directory should have test.txt
ls#step3 git clean
git clean -n
git clean -f
git reset
This command is mostly used by people to recover their code. If you want to rollback to a specific commit hash, then reset is the best command, BUT reset is pretty dangerous.
There are three different types of reset: hard
soft
, and mixed
. Default reset used mixed
. Please refer below picture to be more a clear understanding.
Syntax:
default is mixed:
git reset
git reset #default mixed
git reset [--hard| --soft] HEAD~N
git reset [--hard| --soft] <commit id>
Let me show you an example:
- git reset using mixed AND soft
You can see in the beginning I have committed the message, using the git log you can see there is a commit hash. Then I use git reset
it using the default which is reset mixed, it will roll back from commit message and turn to unstage status.
As you can see above soft and mixed work alike, just soft will roll back to stage status, whereas mixed will move back to unstage.
So let review the process again:
local work place (unstage)=>stage=>commit(local repository)=>push(remote repository)
- git hard reset
Using the same example as above, when I use reset --hard
the file will return to the last commit status. When I use the reset, you can see the status, it has nothing, it because the file is been recovered to the last commit. You can see 1.py the print("RESET TEST1")
is been removed. So using the hard reset is pretty dangerous, but if you accidentally use it, we still are able to recover back using reflog the
command; everything on git is able to recover so please don’t worry.
Conclusion
These are the common git command I use often, but there are many more advanced and complicated git commands. Some commands I didn’t mention in this post, which I don’t use frequently. I wrote another post with many commands in this post, if you’re interested please refer to it.
Last but not least, there are many flag options in git command, which I didn’t mention if you check in git official website or documentation. I hope these commands will help you, which cover basic and intermediate levels. So I am pretty sure that you know most of the commands I mention which think it’s enough.
Update 2023 August
It’s been a long time didn’t add an init repository in GitHub, and released that git’s default master branch is main. I was stuck and it might confuse people, and would like to show the fastest step. I find it on Google but pretty exhausted to search instead I ask chatGPT for all the stuff and would like to share it with people. If you felt lazy reading the above, you can try with below for a shortcut.
Step1: Create a New Local Repository and other basic stuff
Create a New Local Branch: Open your terminal or command prompt and navigate to the directory where you want to create your repository.
Initialize Git Repository:Initialize a new Git repository in the directory.
git init
Add and Commit Changes: Create or add files to your repository, and then commit your changes.
Step2: Create a New GitHub Repository:
Use the GitHub API to create a new repository. Replace
YOUR_ACCESS_TOKEN
with your actual GitHub personal access token andyour-username
with your GitHub username.
You don't have to go to GitHub website instead use this command:
curl -H "Authorization: token YOUR_ACCESS_TOKEN" -d '{"name":"my-new-repo"}' https://api.github.com/user/repos
But you can also go to GitHub page to add the repository
Step4: Add Remote Repository: Create a new repository on GitHub (or any other Git hosting service) and get the repository URL.
Step5: Fetch and Pull Remote Changes: Fetch any remote changes (this step ensures your local repository is up-to-date before pushing).
Step6:Push Changes to Remote: Push your changes from the local “main” branch to the remote “main” branch.
Most of the step actually I had mentioned before but step 2 is new, I didn’t notice we can create a new repository with a token key and GitHub API, that is convenient. Share with people.